vue.extend()
时间: 2024-05-15 10:19:36 浏览: 136
Vue.extend() is a method in Vue.js that creates a new Vue constructor with the options passed to it. It allows us to create reusable components and extend the functionality of existing components.
Here is an example of how to use Vue.extend():
```javascript
const MyComponent = Vue.extend({
template: '<div>{{ message }}</div>',
data() {
return {
message: 'Hello World!'
}
}
})
// create a new instance of MyComponent
const vm = new MyComponent()
// mount the instance to an element on the page
vm.$mount('#app')
```
In this example, we define a new component called MyComponent using Vue.extend(). It has a template that will render a message, and some data to store the message. We create a new instance of MyComponent and mount it to an HTML element on the page using the $mount() method.
Using Vue.extend() allows us to create reusable components that can be used throughout our application. We can define the component options once and then create instances of the component whenever we need them.
阅读全文