VUE注冊全局組件和局部組件過程解析
這篇文章主要介紹了VUE注冊全局組件和局部組件過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
全局組件
第一步:在components文件夾下建立一個子文件Users.vue
<template>
<div class="users">
{{msg}}
</div>
</template>
<script>
export default {
name: 'users',
data () {
return {
msg: '用戶名'
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
第二步:在main.js中進行全局注冊
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
// 全局配置組件第一步 Users是文件夾的名字
import Users from './components/Users'
Vue.config.productionTip = false
// 全局注冊組件第二部 users是
Vue.component('users',Users)
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
第三步:在對應(yīng)的文件中引用
<template>
<div id="app">
<!-- <router-view/>是子路由視圖 -->
<!-- <router-view/> -->
<p>{{title}}</p>
<users></users>
</div>
</template>
<script>
export default {
name: 'App',
data(){
return{
title:"users是全局組件的實例化的名字"
}
}
}
</script>
<style>
</style>
局部組件
在components文件夾下新建一個子組件Users.vue文件,然后在對應(yīng)的文件中引用
<template>
<div id="app">
<!-- <router-view/>是子路由視圖 -->
<!-- <router-view/> -->
<p>{{title}}</p>
<users></users>
</div>
</template>
<script>
/*局部注冊組件*/
import Users from './components/Users'
export default {
name: 'App',
data(){
return{
title:"users是全局組件的實例化的名字"
}
},
components:{
Users,
}
}
</script>
<style>
</style>
或者
<template>
<div id="app">
<app-header></app-header>
<users></users>
<app-footer></app-footer>
</div>
</template>
<script>
import Users from './components/Users'
import Header from './components/Header'
import Footer from './components/Footer'
export default {
name: 'App',
data(){
return{
title:"users是全局組件的實例化的名字"
}
},
components:{
'users':Users,
'app-header':Header,
'app-footer':Footer
}
}
</script>
<style>
</style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
elementui的table根據(jù)是否符合需求合并列的實現(xiàn)代碼
這篇文章主要介紹了elementui的table根據(jù)是否符合需求合并列的實現(xiàn)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03
Vue單頁面應(yīng)用中實現(xiàn)Markdown渲染
這篇文章主要介紹了Vue單頁面應(yīng)用中如何實現(xiàn)Markdown渲染,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2021-02-02
詳解如何使用Object.defineProperty實現(xiàn)簡易的vue功能
這篇文章主要為大家介紹了如何使用Object.defineProperty實現(xiàn)簡易的vue功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04

