vue如何在main.js中配置全局的通用公共組件
在main.js中配置全局的通用公共組件
1、在main.js中直接引入并注冊
main.js引入并注冊
import Loading from './components/Loading.vue'; Vue.component('Loading', Loading);
2、頁面中使用組件
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)提供給我們
使用的組件;前端也有像類似于后端的組件概念。
組件的語法格式:
全局組件
Vue.component(‘組件名’,{代碼的定義})
1.組件名稱:
- 羊肉串法:my-comp-name ->
- 駱駝/pascal法: orderItem- 或
2.代碼的定義
- 2.1 template:‘HTML代碼’
- 2.2 template: 多行代碼,反引號,是ECMAScript6之后提供給我們的符號;
- 2.3 template:外部定義的方式;
三種方式定義分別如下:
第一種方式,單引號(或雙引號)
<!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> <!-- 做下第一個組件的案例,后續(xù)前后端 開發(fā)會用到! --> <body> <div id="app"> <h3>{{msg}}</h3> <order-item></order-item> </div> <script> //1.需要針對order-item這個組件做一個定義,之前沒有定義的; Vue.component('order-item', { template: '<ul><li>今天愚人節(jié)上課</li></ul>' } ); let vm = new Vue({ el: '#app', data: { msg: '全局組件的定義!' } }); </script> </body> </html>
第二種方式反引號
<!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>反引號的全局組件</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.全局組件的定義;做個小作業(yè)做下測試;涵蓋了:組件 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é)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vuex unknown action type報(bào)錯問題及解決
這篇文章主要介紹了Vuex unknown action type報(bào)錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Vue2響應(yīng)式系統(tǒng)之set和delete
這篇文章主要介紹了Vue2響應(yīng)式系統(tǒng)之set和delete,通過為對象收集依賴,將對象、數(shù)組的修改、刪除也變成響應(yīng)式的了,同時為用戶提供了和方法,下文詳細(xì)介紹需要的朋友可以參考一下2022-04-04Electron中打包應(yīng)用程序及相關(guān)報(bào)錯問題的解決
這篇文章主要介紹了Electron中打包應(yīng)用程序及相關(guān)報(bào)錯問題的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Vue Echarts實(shí)現(xiàn)圖表的動態(tài)適配以及如何優(yōu)化
這篇文章主要介紹了Vue Echarts實(shí)現(xiàn)圖表的動態(tài)適配以及如何優(yōu)化,在實(shí)際的前端開發(fā)過程中,動態(tài)適配是一個非常重要的問題,在數(shù)據(jù)可視化的場景下,圖表的動態(tài)適配尤為重要,需要的朋友可以參考下2023-05-05vue實(shí)現(xiàn)歌手列表字母排序下拉滾動條側(cè)欄排序?qū)崟r更新
這篇文章主要介紹了vue實(shí)現(xiàn)歌手列表字母排序,下拉滾動條側(cè)欄排序?qū)崟r更新,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05Vue?elementUI表單嵌套表格并對每行進(jìn)行校驗(yàn)詳解
element-ui中有詳細(xì)的各種表格及表格方法,下面這篇文章主要給大家介紹了關(guān)于Vue?elementUI表單嵌套表格并對每行進(jìn)行校驗(yàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01