Vue單文件組件開(kāi)發(fā)實(shí)現(xiàn)過(guò)程詳解
第一步:配置環(huán)境
安裝cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org
安裝@vue/cli
cnpm install -g @vue/cli
檢查版本是否正確
vue --version

使用vue.server和vue.build對(duì)*.vue文件進(jìn)行快速原型開(kāi)發(fā),需要安裝vue serve
cnpm install -g @vue/cli-service-global
新建一個(gè)App.vue文件測(cè)試安裝是否成功:
<template>2 <h1>Hello world!</h1>3 </template>
在該文件當(dāng)前路徑運(yùn)行:
vue serve App.vue
打開(kāi)瀏覽器輸入localhost:8080看到如下畫(huà)面則運(yùn)行成功

環(huán)境安裝到此結(jié)束,接下來(lái)用一個(gè)簡(jiǎn)單案例來(lái)學(xué)習(xí)vue的單文件組件開(kāi)發(fā)。
第二步:簡(jiǎn)單案例實(shí)戰(zhàn)
以一個(gè)物品清單為例:

該案例由4個(gè)組件構(gòu)成,分別是:
1. addItem.vue 添加物品
2. item.vue 物品實(shí)例
3. items.vue 物品列表
4. changeTitle 改變標(biāo)題
首先,創(chuàng)建一個(gè)項(xiàng)目demo:
vue create demo
項(xiàng)目默認(rèn)目錄如下,啟動(dòng)主頁(yè)在public, vue源碼(包括組件)都存放到src

然后分別編寫(xiě)各組件代碼
1. addItem.vue:
<template>
<div class="input-group">
<input type="text" class="form-control" placeholder="add shopping list item" v-model="newItem">
<span class="input-group-btn">
<button class="btn btn-primary" @click="emitAdd">
<i class="fa fa-plus-square-o fa-lg"> </i><span>Add</span>
</button>
</span>
</div>
</template>
<script>
export default {
data() {
return {
newItem: ''
}
},
methods: {
emitAdd() {
this.$emit('addItem', this.newItem);
}
}
}
</script>
<style>
</style>
2. item.vue:
<template>
<li :class="{'removed': item.checked}" class="list-group-item">
<div class="checkbox">
<label>
<input type="checkbox" v-model="item.checked">
<span>{{ item.text }}</span>
</label>
</div>
</li>
</template>
<script>
export default {
props: ['item']
}
</script>
<style>
.removed {
color: gray;
}
.removed span {
text-decoration: line-through;
}
</style>
3. items.vue:
<script>
import item from './item'
export default {
props: ['items'],
components: {
item
}
}
</script>
<template>
<ul class="list-group">
<item v-for="item in items" :key="item.id" :item="item"></item>
</ul>
</template>
<style>
</style>
4. changeTitle.vue:
<template>
<div>
<em>Change the title here:</em>
<input type="text" :value="title" @input="onInput">
</div>
</template>
<script>
export default {
props: ['title'],
methods: {
onInput(event) {
this.$emit('input', event.target.value);
}
}
}
</script>
最后修改App.vue,導(dǎo)入上面的組件:
<template>
<div id="app" class="container">
<h1>{{ title }}</h1>
<add-item @addItem="add"></add-item><br>
<items :items="items"></items>
<div class="footer">
<hr>
<change-title :title="title" v-model="title"></change-title>
</div>
</div>
</template>
<script>
import addItem from './components/addItem'
import items from './components/items'
import changeTitle from './components/changeTitle'
export default {
name: 'app',
components: {
addItem,
items,
changeTitle
},
data() {
return {
items: [
{id: 1, text: 'Bananas', checked: true},
{id: 2, text: 'Apples', checked: false}
],
title: 'My Items List'
}
},
methods: {
add(text) {
this.items.push({
text: text,
checked: false
});
}
}
}
</script>
<style>
</style>
需要注意的是:每個(gè)組件必須只有一個(gè)根元素。我這里需要在public/index.html引入bootstrap樣式和font-awesome圖標(biāo)字體。
運(yùn)行程序:
cnpm run serve
最后附上運(yùn)行截圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解讓sublime text3支持Vue語(yǔ)法高亮顯示的示例
本篇文章主要介紹了讓sublime text3支持Vue語(yǔ)法高亮顯示的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
vue 二維碼長(zhǎng)按保存和復(fù)制內(nèi)容操作
這篇文章主要介紹了vue 二維碼長(zhǎng)按保存和復(fù)制內(nèi)容操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
解決ant design vue中樹(shù)形控件defaultExpandAll設(shè)置無(wú)效的問(wèn)題
這篇文章主要介紹了解決ant design vue中樹(shù)形控件defaultExpandAll設(shè)置無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
解決vue admin element noCache設(shè)置無(wú)效的問(wèn)題
今天小編就為大家分享一篇解決vue admin element noCache設(shè)置無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Vue使用Pinia輕松實(shí)現(xiàn)狀態(tài)管理
pinia,一個(gè)基于Vue3的狀態(tài)管理庫(kù),它可以幫助開(kāi)發(fā)人員管理Vue應(yīng)用程序的狀態(tài),本文主要為大家介紹了Pinia的用法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06
詳解vue-cli項(xiàng)目中用json-sever搭建mock服務(wù)器
這篇文章主要介紹了詳解vue-cli項(xiàng)目中用json-sever搭建mock服務(wù)器,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Vue.js集成Word實(shí)現(xiàn)在線編輯功能
在現(xiàn)代Web應(yīng)用中,集成文檔編輯功能變得越來(lái)越常見(jiàn),特別是在協(xié)作環(huán)境中,能夠直接在Web應(yīng)用內(nèi)編輯Word文檔可以極大地提高工作效率,本文將詳細(xì)介紹如何在Vue.js項(xiàng)目中集成Word在線編輯功能,需要的朋友可以參考下2024-08-08
el-input寬度跟隨輸入內(nèi)容自適應(yīng)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于el-input寬度跟隨輸入內(nèi)容自適應(yīng)的實(shí)現(xiàn)方法,我們?cè)賹?shí)際應(yīng)用中可能需要input文本框能夠根據(jù)輸入字符的所占據(jù)的寬度自動(dòng)調(diào)節(jié)尺寸,需要的朋友可以參考下2023-08-08
elementPlus?的el-select在提示框關(guān)閉時(shí)自動(dòng)彈出的問(wèn)題解決
這篇文章主要介紹了elementPlus?的el-select在提示框關(guān)閉時(shí)自動(dòng)彈出閉時(shí)自動(dòng)彈出的問(wèn)題,主要問(wèn)題就是因?yàn)閒ilterable屬性,根本解決方案是選中的時(shí)候讓他失去焦點(diǎn)?el-select有一個(gè)visible-change事件,下拉框出現(xiàn)/隱藏時(shí)觸發(fā),感興趣的朋友跟隨小編一起看看吧2024-01-01

