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
Table of contents 1. Overview 2. Application Exam...
This article shares the specific code of react+an...
0. Preliminary preparation Disable secure boot in...
This method was edited on February 7, 2021. The v...
Table of contents Problem Description Cause Analy...
Table of contents 1 Node.js method of sending ema...
The <canvas> element is designed for client...
Table of contents 1. Connect to Tencent Cloud Ser...
This article shares the installation and configur...
Recently, during the development process, the MyS...
Table of contents 1. Add users 2. Change the user...
Background Information I've been rereading so...
1. Two words at the beginning Hello everyone, my ...
This article shares a draggable login box impleme...
This article shares the specific code of JavaScri...