vue.js既然是框架,那就不能只是簡(jiǎn)單的完成數(shù)據(jù)模板引擎的任務(wù),它還提供了頁(yè)面布局的功能。本文詳細(xì)介紹使用vue.js進(jìn)行頁(yè)面布局的強(qiáng)大工具,vue.js組件系統(tǒng)。
每一個(gè)新技術(shù)的誕生,都是為了解決特定的問題。組件的出現(xiàn)就是為了解決頁(yè)面布局等等一系列問題。vue中的組件分為兩種,全局組件和局部組件。
通過Vue.component()創(chuàng)建一個(gè)全局組件之后,我們可以在一個(gè)通過 new Vue
創(chuàng)建的 Vue 根實(shí)例中,把這個(gè)組件作為自定義元素來使用。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.min.js">script>
head>
<body>
<div id="app">
<global_component>global_component>
div>
<script>
// 第一步,注冊(cè)
Vue.component("global_component", {
template: `
<div>
<h2>Hello Vue</h2>
</div>
`
});
new Vue({
el: "#app",
});
script>
body>
html>
因?yàn)榻M件是可復(fù)用的 Vue 實(shí)例,所以它們與 new Vue
接收相同的選項(xiàng),例如 data
、computed
、watch
、methods
以及生命周期鉤子等。僅有的例外是像 el
這樣根實(shí)例特有的選項(xiàng)。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.min.js">script>
head>
<body>
<div id="app">
<global_component>global_component>
div>
<script>
// 第一步,注冊(cè)
Vue.component("global_component", {
data: function () {
return {
count: 0
}
},
template: `<button v-on:click="count++">You clicked me {{ count }} times.</button>`
});
new Vue({
el: "#app",
});
script>
body>
html>
每個(gè)實(shí)例維護(hù)自己的一份獨(dú)立的數(shù)據(jù)。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.min.js">script>
head>
<body>
<div id="app">
<global_component>global_component>
<global_component>global_component>
<global_component>global_component>
div>
<script>
// 第一步,注冊(cè)
Vue.component("global_component", {
data: function () {
return {
count: 0
}
},
template: `<button v-on:click="count++">You clicked me {{ count }} times.</button>`
});
new Vue({
el: "#app",
});
script>
body>
html>
注意當(dāng)點(diǎn)擊按鈕時(shí),每個(gè)組件都會(huì)各自獨(dú)立維護(hù)它的 count
。因?yàn)槟忝坑靡淮谓M件,就會(huì)有一個(gè)它的新實(shí)例被創(chuàng)建。
data必須是一個(gè)函數(shù),因此每個(gè)實(shí)例可以維護(hù)一份被返回對(duì)象的獨(dú)立的拷貝, 也可以寫成如下形式
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.min.js">script>
head>
<body>
<div id="app">
<global_component>global_component>
<global_component>global_component>
<global_component>global_component>
div>
<script>
// 第一步,注冊(cè)
Vue.component("global_component", {
data(){
return {
count: 0
}
},
template: `<button v-on:click="count++">You clicked me {{ count }} times.</button>`
});
new Vue({
el: "#app",
});
script>
body>
html>
全局注冊(cè)往往是不夠理想的。比如,如果你使用一個(gè)像 webpack 這樣的構(gòu)建系統(tǒng),全局注冊(cè)所有的組件意味著即便你已經(jīng)不再使用一個(gè)組件了,它仍然會(huì)被包含在你最終的構(gòu)建結(jié)果中。這造成了用戶下載的 JavaScript 的無謂的增加。
全局組件始終是存在的,除非程序結(jié)束,如果組件越來越大,那么程序所占用的空間和消耗的性能就會(huì)更大。
所以我們需要局部組件。不用的時(shí)候,被垃圾回收。
局部組件的第一種使用方式
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.min.js">script>
head>
<body>
<div id="component-demo">
<my-header>my-header>
div>
<script>
// 定義一個(gè)局部組件,其實(shí)就是一個(gè)變量,它是一個(gè)object類型
// 屬性與全局組件是一樣的
let Header = {
template: `
<button @click="count++">{{ count }}</button>
`,
data() {
return {
count: 0
}
}
};
new Vue({
el: "#component-demo",
// 第二部,需要在根實(shí)例當(dāng)中使用它
components: {
my-header: Header
}
});
script>
body>
html>
局部組件的第二種使用方式
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.min.js">script>
head>
<body>
<div id="component-demo">
div>
<script>
// 定義一個(gè)局部組件,其實(shí)就是一個(gè)變量,它是一個(gè)object類型
// 屬性與全局組件是一樣的
let Header = {
template: `
<button @click="count++">{{ count }}</button>
`,
data() {
return {
count: 0
}
}
};
new Vue({
el: "#component-demo",
// 第二種使用方式,不會(huì)將div渲染進(jìn)DOM,以template為根元素
template: `<my-header></my-header>`,
// 第二步,需要在根實(shí)例當(dāng)中使用它
components: {
my-header: Header
}
});
script>
body>
html>
對(duì)于 components
對(duì)象中的每個(gè)屬性來說,其屬性名就是自定義元素的名字,其屬性值就是這個(gè)組件的選項(xiàng)對(duì)象。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.min.js">script>
<style>
body {
margin: 0;
}
.box {
width: 100%;
height: 50px;
background-color: #2aabd2;
}
style>
head>
<body>
<div id="component-demo">
div>
<script>
// 定義一個(gè)局部組件,其實(shí)就是一個(gè)變量,它是一個(gè)object類型
// 這個(gè)對(duì)象的屬性與全局組件是一樣的(除el屬性外)
let Fcontent = {
template: `
<div>
<span>這是頭條</span>
</div>
`
};
let Header = {
template: `
<div v-bind:class={box: isBox}>
<button @click="count++">{{ count }}</button>
<first-content></first-content>
</div>
`,
data() {
return {
count: 0,
isBox: true
}
},
components: {
first-content: Fcontent
}
};
new Vue({
el: "#component-demo",
// 第二種使用方式,不會(huì)將div渲染進(jìn)DOM,以template為根元素
template: `<my-header></my-header>`,
// 第二步,需要在根實(shí)例當(dāng)中使用它
components: {
my-header: Header
}
});
script>
body>
html>
注:
1.注意寫組件標(biāo)簽
2.每個(gè)組件的template只識(shí)別一個(gè)作用域塊
在父組件中給子組件綁定屬性,子組件通過props=["屬性名稱"]
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.min.js">script>
<style>
body {
margin: 0;
}
.box {
width: 100%;
height: 50px;
background-color: #2aabd2;
}
style>
head>
<body>
<div id="component-demo">
div>
<script>
// 定義一個(gè)局部組件,其實(shí)就是一個(gè)變量,它是一個(gè)object類型
// 屬性與全局組件是一樣的
let Fcontent = {
template: `
<div>
<span>這是頭條</span>
{{ fdata }}
</div>
`,
props: [fdata]
};
let Header = {
template: `
<div v-bind:class={box: isBox}>
<button @click="count++">{{ count }}</button>
<first-content :fdata="fathData"></first-content>
</div>
`,
data() {
return {
count: 0,
isBox: true,
fathData: "我是你爸爸~~~"
}
},
components: {
first-content: Fcontent
}
};
new Vue({
el: "#component-demo",
// 第二種使用方式,不會(huì)將div渲染進(jìn)DOM,以template為根元素
template: `<my-header></my-header>`,
// 第二步,需要在根實(shí)例當(dāng)中使用它
components: {
my-header: Header
}
});
script>
body>
html>
父組件在mounted的時(shí)候,監(jiān)聽一個(gè)自定義事件。
給子組件綁定一個(gè)click事件之后,通過內(nèi)建的方法$emit在父組件上觸發(fā)一個(gè)事件,這個(gè)時(shí)間就是父組件監(jiān)聽的自定義事件。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="../statics/vue.min.js">script>
<style>
body {
margin: 0;
}
.box {
width: 100%;
height: 50px;
background-color: #2aabd2;
}
style>
head>
<body>
<div id="component-demo">
div>
<script>
// 定義一個(gè)局部組件,其實(shí)就是一個(gè)變量,它是一個(gè)object類型
// 屬性與全局組件是一樣的
let Fcontent = {
template: `
<div>
<button v-on:click="myClick">放大父組件字體</button>
</div>
`,
methods: {
myClick: function () {
console.log(this);
this.$emit(change-font
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/1541.html
摘要:五六月份推薦集合查看最新的請(qǐng)點(diǎn)擊集前端最近很火的框架資源定時(shí)更新,歡迎一下。蘇幕遮燎沈香宋周邦彥燎沈香,消溽暑。鳥雀呼晴,侵曉窺檐語(yǔ)。葉上初陽(yáng)乾宿雨,水面清圓,一一風(fēng)荷舉。家住吳門,久作長(zhǎng)安旅。五月漁郎相憶否。小楫輕舟,夢(mèng)入芙蓉浦。 五、六月份推薦集合 查看github最新的Vue weekly;請(qǐng)::點(diǎn)擊::集web前端最近很火的vue2框架資源;定時(shí)更新,歡迎 Star 一下。 蘇...
摘要:五六月份推薦集合查看最新的請(qǐng)點(diǎn)擊集前端最近很火的框架資源定時(shí)更新,歡迎一下。蘇幕遮燎沈香宋周邦彥燎沈香,消溽暑。鳥雀呼晴,侵曉窺檐語(yǔ)。葉上初陽(yáng)乾宿雨,水面清圓,一一風(fēng)荷舉。家住吳門,久作長(zhǎng)安旅。五月漁郎相憶否。小楫輕舟,夢(mèng)入芙蓉浦。 五、六月份推薦集合 查看github最新的Vue weekly;請(qǐng)::點(diǎn)擊::集web前端最近很火的vue2框架資源;定時(shí)更新,歡迎 Star 一下。 蘇...
摘要:導(dǎo)語(yǔ)承接上文實(shí)戰(zhàn)之后臺(tái)管理系統(tǒng)開發(fā)一在上一篇文章中,我詳細(xì)敘述了如何創(chuàng)建項(xiàng)目框架和引入各種后臺(tái)常用插件,做好這些準(zhǔn)備工作后,我們就可以著手進(jìn)行頁(yè)面的開發(fā)了。如果傳入的數(shù)據(jù)不符合規(guī)格,會(huì)發(fā)出警告。 1. 導(dǎo)語(yǔ) 承接上文:Vue 2.x 實(shí)戰(zhàn)之后臺(tái)管理系統(tǒng)開發(fā)(一) 在上一篇文章中,我詳細(xì)敘述了如何創(chuàng)建項(xiàng)目框架和引入各種后臺(tái)常用插件,做好這些準(zhǔn)備工作后,我們就可以著手進(jìn)行頁(yè)面的開發(fā)了。在開...
摘要:前端技術(shù)棧還是非常龐大的,為了能夠借助已經(jīng)存在的輪子來造出一輛車,所以我選擇了進(jìn)行實(shí)踐。狀態(tài)的管理的狀態(tài)管理依靠完成,用其來管理的所有組件狀態(tài)。私有云客戶端打造主頁(yè)面首先是主頁(yè)面,可以打開任何一個(gè)云主機(jī)系統(tǒng)的頁(yè)面看,基本類似。 showImg(https://segmentfault.com/img/remote/1460000013930354); 【利用K8S技術(shù)棧打造個(gè)人私有...
閱讀 740·2023-04-25 19:43
閱讀 3986·2021-11-30 14:52
閱讀 3816·2021-11-30 14:52
閱讀 3873·2021-11-29 11:00
閱讀 3809·2021-11-29 11:00
閱讀 3907·2021-11-29 11:00
閱讀 3584·2021-11-29 11:00
閱讀 6197·2021-11-29 11:00