Import: Due to project requirements, we will encapsulate some of the same code into components and import them where they are needed. Usually, we customize components, and usually use them in the form of method 2. Today, we will introduce how to use method 1. Coding ImplementationUsing components as plugins// Import the component to be displayed import mymodel from '../components/mymodel.vue' export default { install: function (Vue) { // 1.0 Get the constructor of mymodel component object const Mymodel = Vue.extend(mymodel) // Add a method $model to all vue instances Vue.prototype.$model = function () { // To display a component: mymodel // 2.0 Create a component object const obj = new Mymodel() // 3.0 Display the component obj.show = true // 4.0 Get the html structure of the component object const html = obj.$mount().$el // 5.0 Render the html structure to the page document.body.append(html) } } } use<template> <div> <h3>Call with normal component method</h3> <button @click="fn1">show Model</button> <!-- <mymodel :value="show" @input="val => (show = val)"></mymodel> --> <mymodel v-model="show"></mymodel> <!-- sync: Parameters are passed into the component: xxx Received event from the component: update:xxx The event will automatically modify xxx --> <!-- v-model: Parameters are passed into the component: value Events are received from the component: input events will automatically modify value --> <h3>Calling with js</h3> <button @click="fn2">show Model</button> </div> </template> <script> import mymodel from '../../components/mymodel.vue' export default { data () { return { show:false } }, methods: { fn1 () { this.show = true }, fn2 () { // Display the component directly through js method this.$model() } }, components: mymodel: mymodel } } </script> <style></style> The above is the details of the web interview Vue custom components and calling methods. For more information about web interview Vue, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Detailed explanation of how to adjust Linux command history
>>: Reasons why MySQL cancelled Query Cache
introduce Monitors the health of HTTP servers in ...
During the development activity, I encountered a ...
Table of contents variable Data Types Extension P...
"Grand" are probably the two words that ...
This article uses examples to describe how to bac...
Background of the accident: A few days ago, due t...
Preface Different script execution methods will r...
0. System requirements CPU I5-10400F or above Mem...
If a div sets background-color and background-ima...
Table of contents Scenario Code Implementation Su...
Table of contents 1. Integrate Ant Design Vue 2. ...
Related articles: Beginners learn some HTML tags ...
Server: Ubuntu Server 16.04 LSS Client: Ubuntu 16...
Table of contents 1. Baidu Encyclopedia 1. MySQL ...
1. Modify the docker configuration file and open ...