vue如何在main.js中配置全局的通用公共組件
在main.js中配置全局的通用公共組件
1、在main.js中直接引入并注冊(cè)
main.js引入并注冊(cè)
import Loading from './components/Loading.vue'; Vue.component('Loading', Loading);
2、頁(yè)面中使用組件
html中使用:
<template> ? ?<div class='wrapper'> ? ? ? ?<Loading ?:ready="ready"></Loading> ? ?</div> </template> export default { ? name: "index", ? data() { ? ? return { ? ? ? ready: false, ? ? }; ? }, ? created() { ? ?// 這里是為了模擬改loading的顯示和隱藏用的函數(shù) ? ? setTimeout(() => { ? ? ? this.ready = true ? ? }, 3000); ? }, ? computed: {}, ? methods: { ? }, }; </script>
vue.js全局組件的三種方式
組件:button imageview listview等這些都是AS系統(tǒng)提供給我們
使用的組件;前端也有像類似于后端的組件概念。
組件的語(yǔ)法格式:
全局組件
Vue.component(‘組件名’,{代碼的定義})
1.組件名稱:
- 羊肉串法:my-comp-name ->
- 駱駝/pascal法: orderItem- 或
2.代碼的定義
- 2.1 template:‘HTML代碼’
- 2.2 template: 多行代碼,反引號(hào),是ECMAScript6之后提供給我們的符號(hào);
- 2.3 template:外部定義的方式;
三種方式定義分別如下:
第一種方式,單引號(hào)(或雙引號(hào))
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>全局組件</title> <script src="../vue.js"></script> </head> <!-- 做下第一個(gè)組件的案例,后續(xù)前后端 開發(fā)會(huì)用到! --> <body> <div id="app"> <h3>{{msg}}</h3> <order-item></order-item> </div> <script> //1.需要針對(duì)order-item這個(gè)組件做一個(gè)定義,之前沒(méi)有定義的; Vue.component('order-item', { template: '<ul><li>今天愚人節(jié)上課</li></ul>' } ); let vm = new Vue({ el: '#app', data: { msg: '全局組件的定義!' } }); </script> </body> </html>
第二種方式反引號(hào)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../vue.js"></script> </head> <body> <div id="app"> <order-item></order-item> </div> <div id="app1"> <order-item></order-item> </div> <script> //1.定義組件; Vue.component('order-item', { template: ` <div> <h1>反引號(hào)的全局組件</h1> <p>京東特賣<p> <ul><li>LV包包</li><li>GuCCI</li></ul> </div> ` }) //2.綁定 new Vue({ el: '#app' }); new Vue({ el: '#app1' }); </script> </body> </html>
第三種方式外部ID
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="../vue.js"></script> </head> <body> <div id="app"> <btn-counter></btn-counter> </div> <template id="btn-counter"> <button @click="add">單擊{{count}}次</button> </template> <script> //1.全局組件的定義;做個(gè)小作業(yè)做下測(cè)試;涵蓋了:組件 data methods三種方式; Vue.component('btn-counter', { template: '#btn-counter', data: function () { return { count: 0 } }, methods: { add: function () { this.count++ } } }) //2.綁定; let vm1 = new Vue({ el: '#app' }); </script> </body> </html>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解vue-property-decorator使用手冊(cè)
這篇文章主要介紹了vue-property-decorator使用手冊(cè),文中較詳細(xì)的給大家介紹了他們的用法,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07Vuex unknown action type報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了Vuex unknown action type報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Vue2響應(yīng)式系統(tǒng)之set和delete
這篇文章主要介紹了Vue2響應(yīng)式系統(tǒng)之set和delete,通過(guò)為對(duì)象收集依賴,將對(duì)象、數(shù)組的修改、刪除也變成響應(yīng)式的了,同時(shí)為用戶提供了和方法,下文詳細(xì)介紹需要的朋友可以參考一下2022-04-04Electron中打包應(yīng)用程序及相關(guān)報(bào)錯(cuò)問(wèn)題的解決
這篇文章主要介紹了Electron中打包應(yīng)用程序及相關(guān)報(bào)錯(cuò)問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Vue Echarts實(shí)現(xiàn)圖表的動(dòng)態(tài)適配以及如何優(yōu)化
這篇文章主要介紹了Vue Echarts實(shí)現(xiàn)圖表的動(dòng)態(tài)適配以及如何優(yōu)化,在實(shí)際的前端開發(fā)過(guò)程中,動(dòng)態(tài)適配是一個(gè)非常重要的問(wèn)題,在數(shù)據(jù)可視化的場(chǎng)景下,圖表的動(dòng)態(tài)適配尤為重要,需要的朋友可以參考下2023-05-05vue實(shí)現(xiàn)歌手列表字母排序下拉滾動(dòng)條側(cè)欄排序?qū)崟r(shí)更新
這篇文章主要介紹了vue實(shí)現(xiàn)歌手列表字母排序,下拉滾動(dòng)條側(cè)欄排序?qū)崟r(shí)更新,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Vue?elementUI表單嵌套表格并對(duì)每行進(jìn)行校驗(yàn)詳解
element-ui中有詳細(xì)的各種表格及表格方法,下面這篇文章主要給大家介紹了關(guān)于Vue?elementUI表單嵌套表格并對(duì)每行進(jìn)行校驗(yàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01