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

Detailed explanation of Nginx passively checking the server's survival status

introduce Monitors the health of HTTP servers in ...

How to use border-image to implement text bubble border sample code

During the development activity, I encountered a ...

Detailed explanation of Javascript basics

Table of contents variable Data Types Extension P...

How to create a stylish web page design (graphic tutorial)

"Grand" are probably the two words that ...

Analysis of MySQL data backup and recovery implementation methods

This article uses examples to describe how to bac...

Solution to uninstalling Python and yum in CentOs system

Background of the accident: A few days ago, due t...

Detailed process of installing and deploying onlyoffice in docker

0. System requirements CPU I5-10400F or above Mem...

Implementation of select multiple data loading optimization in Element

Table of contents Scenario Code Implementation Su...

Vue3 (Part 2) Integrating Ant Design Vue

Table of contents 1. Integrate Ant Design Vue 2. ...

HTML Tutorial: Collection of commonly used HTML tags (6)

Related articles: Beginners learn some HTML tags ...

How to configure mysql on ubuntu server and implement remote connection

Server: Ubuntu Server 16.04 LSS Client: Ubuntu 16...

Practical MySQL + PostgreSQL batch insert update insertOrUpdate

Table of contents 1. Baidu Encyclopedia 1. MySQL ...

How to connect idea to docker to achieve one-click deployment

1. Modify the docker configuration file and open ...