vue如何在main.js中配置全局的通用公共組件
更新時間:2023年01月13日 14:36:59 作者:牛先森家的牛奶
這篇文章主要介紹了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>
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vuex unknown action type報錯問題及解決
這篇文章主要介紹了Vuex unknown action type報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Vue Echarts實現(xiàn)圖表的動態(tài)適配以及如何優(yōu)化
這篇文章主要介紹了Vue Echarts實現(xiàn)圖表的動態(tài)適配以及如何優(yōu)化,在實際的前端開發(fā)過程中,動態(tài)適配是一個非常重要的問題,在數(shù)據(jù)可視化的場景下,圖表的動態(tài)適配尤為重要,需要的朋友可以參考下2023-05-05
vue實現(xiàn)歌手列表字母排序下拉滾動條側欄排序實時更新
這篇文章主要介紹了vue實現(xiàn)歌手列表字母排序,下拉滾動條側欄排序實時更新,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05

