Web interview Vue custom components and calling methods

Web interview Vue custom components and calling methods

Import:

Due to project requirements, we will encapsulate some of the same code into components and import them where they are needed.
And written in the form of tags,
But in the "vant" component library, the "Dialog popup box" component has 2 ways of use:

Usually, we customize components, and usually use them in the form of method 2. Today, we will introduce how to use method 1.

Coding Implementation

Using 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 four ways to customize Vue components
  • Vue component common method extraction mixin implementation
  • How to abstract a Vue common component
  • Vue implements custom public components and extracts public methods

<<:  Detailed explanation of how to adjust Linux command history

>>:  Reasons why MySQL cancelled Query Cache

Recommend

Summary of fragmented knowledge of Docker management

Table of contents 1. Overview 2. Application Exam...

react+antd.3x implements ip input box

This article shares the specific code of react+an...

The best solution for resetting the root password of MySQL 8.0.23

This method was edited on February 7, 2021. The v...

Difference between MySQL update set and and

Table of contents Problem Description Cause Analy...

Node.js sends emails based on STMP protocol and EWS protocol

Table of contents 1 Node.js method of sending ema...

jQuery uses the canvas tag to draw the verification code

The <canvas> element is designed for client...

A detailed introduction to setting up Jenkins on Tencent Cloud Server

Table of contents 1. Connect to Tencent Cloud Ser...

MySQL 5.7.17 and workbench installation and configuration graphic tutorial

This article shares the installation and configur...

Solution to ONLY_FULL_GROUP_BY error in Mysql5.7 and above

Recently, during the development process, the MyS...

MySQL database terminal - common operation command codes

Table of contents 1. Add users 2. Change the user...

How to monitor Windows performance on Zabbix

Background Information I've been rereading so...

Detailed explanation of Django+Vue+Docker to build an interface testing platform

1. Two words at the beginning Hello everyone, my ...

Native JS to achieve draggable login box

This article shares a draggable login box impleme...