這篇文章將為大家詳細講解有關VUE中怎么注冊全局組件和局部組件,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
全局組件
第一步:在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/>' })
第三步:在對應的文件中引用
<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文件,然后在對應的文件中引用
<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>
關于VUE中怎么注冊全局組件和局部組件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。