vue如何编写组件能够经过Vue.use()使用

通常平时用别人的组件时都是经过import引入而后Vue.use()来使用,那么如何让咱们写的组件也能够用这种方式使用呢?

1.首先新建一个文件夹例如:Home,而后在该文件中新建两个文件Home.vue和index.jsvue

2.在Home.vue中和往常同样如:app

<template>
    <div id="Home">
        <h1>加上点加鸡蛋</h1>
    </div>
</template>
<script>
    export default{
        name:"Home"
    }
</script>

3.接下来是index.js的编写(主要)code

import Home from './Home.vue'
// install方法必须有
export default{
    install:function(Vue) {
        Vue.component('Home',Home);
    }
}

4.接下来就能够在main.js中美美的引入你的组件了component

import Home from './components/Home'
Vue.use(Home)

5.使用(在App.vue中)ip

<div id="app">
    <mt-button type="default">default</mt-button>
    <mt-button type="primary">primary</mt-button>
    <mt-button type="danger">danger</mt-button>
    <Home></Home>
</div>

6.效果展现get

相关文章
相关标签/搜索