VUE注冊全局組件和局部組件過程解析
這篇文章主要介紹了VUE注冊全局組件和局部組件過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
全局組件
第一步:在components文件夾下建立一個(gè)子文件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中進(jìn)行全局注冊
// 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是全局組件的實(shí)例化的名字" } } } </script> <style> </style>
局部組件
在components文件夾下新建一個(gè)子組件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是全局組件的實(shí)例化的名字" } }, 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是全局組件的實(shí)例化的名字" } }, components:{ 'users':Users, 'app-header':Header, 'app-footer':Footer } } </script> <style> </style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3項(xiàng)目中預(yù)覽并打印PDF的兩種方法
最近在項(xiàng)目開發(fā)中碰到一個(gè)需求是在頁面中展示pdf預(yù)覽功能,下面這篇文章主要給大家介紹了關(guān)于Vue3項(xiàng)目中預(yù)覽并打印PDF的兩種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05vue如何移動到指定位置(scrollIntoView)親測避坑
這篇文章主要介紹了vue如何移動到指定位置(scrollIntoView)親測避坑,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Vue實(shí)現(xiàn)boradcast和dispatch的示例
這篇文章主要介紹了Vue實(shí)現(xiàn)boradcast和dispatch的示例,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2020-11-11Vue+ElementUI技巧之自定義表單項(xiàng)label的文字提示方法
這篇文章主要給大家介紹了關(guān)于Vue+ElementUI技巧之自定義表單項(xiàng)label文字提示的相關(guān)資料,文中通過圖文以及代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-02-02Vue Promise解決回調(diào)地獄問題實(shí)現(xiàn)方法
這篇文章主要介紹了Vue Promise解決回調(diào)地獄問題,總的來說這并不是一道難題,那為什么要拿出這道題介紹?拿出這道題真正想要傳達(dá)的是解題的思路,以及不斷優(yōu)化探尋最優(yōu)解的過程。希望通過這道題能給你帶來一種解題優(yōu)化的思路2023-01-01Vue3?KeepAlive實(shí)現(xiàn)原理解析
KeepAlive?是一個(gè)內(nèi)置組件,那封裝一個(gè)組件對于大家來說應(yīng)該不會有太大的困難,它的核心邏輯在于它的?render?函數(shù),它用?map?去記錄要緩存的組件,就是?[key,vnode]?的形式,這篇文章主要介紹了Vue3?KeepAlive實(shí)現(xiàn)原理,需要的朋友可以參考下2022-09-09詳解vuex持久化插件解決瀏覽器刷新數(shù)據(jù)消失問題
這篇文章主要介紹了詳解vuex持久化插件解決瀏覽器刷新數(shù)據(jù)消失問題,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04