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

What are Web Slices?

IE8 new feature Web Slices (Web Slices) Microsoft...

Embedded transplant docker error problem (summary)

After a long period of transplantation and inform...

How to use Cron Jobs to execute PHP regularly under Cpanel

Open the cpanel management backend, under the &qu...

Implementation of debugging code through nginx reverse proxy

background Now the company's projects are dev...

Solve the problem of Tomcat10 Catalina log garbled characters

Running environment, Idea2020 version, Tomcat10, ...

WeChat Mini Programs Implement Star Rating

This article shares the specific code for WeChat ...

The grid is your layout plan for the page

<br /> English original: http://desktoppub.a...

What does href=# mean in a link?

Links to the current page. ------------------- Com...

Native JS to achieve blinds special effects

This article shares a blinds special effect imple...

How to upgrade https under Nginx

Purchase Certificate You can purchase it from Ali...

MySQL index for beginners

Preface Since the most important data structure i...

PyTorch development environment installation tutorial under Windows

Anaconda Installation Anaconda is a software pack...