欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue單文件組件開發(fā)實現(xiàn)過程詳解

 更新時間:2020年07月30日 15:36:32   作者:viewts  
這篇文章主要介紹了Vue單文件組件開發(fā)實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

第一步:配置環(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對*.vue文件進行快速原型開發(fā),需要安裝vue serve

cnpm install -g @vue/cli-service-global

新建一個App.vue文件測試安裝是否成功:

<template>2 <h1>Hello world!</h1>3 </template>

在該文件當(dāng)前路徑運行:

vue serve App.vue

打開瀏覽器輸入localhost:8080看到如下畫面則運行成功

環(huán)境安裝到此結(jié)束,接下來用一個簡單案例來學(xué)習(xí)vue的單文件組件開發(fā)。

第二步:簡單案例實戰(zhàn)

以一個物品清單為例:

該案例由4個組件構(gòu)成,分別是:

1. addItem.vue 添加物品

2. item.vue 物品實例

3. items.vue 物品列表

4. changeTitle 改變標(biāo)題

首先,創(chuàng)建一個項目demo:

vue create demo

項目默認(rèn)目錄如下,啟動主頁在public, vue源碼(包括組件)都存放到src

然后分別編寫各組件代碼

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>

需要注意的是:每個組件必須只有一個根元素。我這里需要在public/index.html引入bootstrap樣式和font-awesome圖標(biāo)字體。

運行程序:

cnpm run serve

最后附上運行截圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue+highCharts實現(xiàn)可選范圍的圖表

    vue+highCharts實現(xiàn)可選范圍的圖表

    這篇文章主要為大家詳細(xì)介紹了vue+highCharts實現(xiàn)可選范圍的圖表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Vue.js實現(xiàn)立體計算器

    Vue.js實現(xiàn)立體計算器

    這篇文章主要為大家詳細(xì)介紹了Vue.js實現(xiàn)立體計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • vue?cli?局部混入mixin和全局混入mixin的過程

    vue?cli?局部混入mixin和全局混入mixin的過程

    這篇文章主要介紹了vue?cli?局部混入mixin和全局混入mixin的過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Nuxt的路由動畫效果案例

    Nuxt的路由動畫效果案例

    這篇文章主要介紹了Nuxt的路由動畫效果案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 詳細(xì)講解如何創(chuàng)建, 發(fā)布自己的 Vue UI 組件庫

    詳細(xì)講解如何創(chuàng)建, 發(fā)布自己的 Vue UI 組件庫

    當(dāng)我們自己開發(fā)了一個 _UI Component_, 需要在多個項目中使用的時候呢? 我們首先想到的可能是直接復(fù)制一份過去對嗎?我們?yōu)槭裁床话l(fā)布一個 UI 組件庫給自己用呢?下面小編和大家來一起學(xué)習(xí)下吧
    2019-05-05
  • vue2.0 子組件改變props值,并向父組件傳值的方法

    vue2.0 子組件改變props值,并向父組件傳值的方法

    下面小編就為大家分享一篇vue2.0 子組件改變props值,并向父組件傳值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Vue3中的h函數(shù)及使用小結(jié)

    Vue3中的h函數(shù)及使用小結(jié)

    這篇文章主要介紹了Vue3中的h函數(shù)及使用小結(jié),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • vue-cli2.0轉(zhuǎn)3.0之項目搭建的詳細(xì)步驟

    vue-cli2.0轉(zhuǎn)3.0之項目搭建的詳細(xì)步驟

    這篇文章主要介紹了vue-cli2.0轉(zhuǎn)3.0之項目搭建的詳細(xì)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 關(guān)于Less在Vue中的使用方法介紹

    關(guān)于Less在Vue中的使用方法介紹

    最近發(fā)現(xiàn)好多小伙伴在面試的過程中會問到vue如何使用less和scss,所以下面這篇文章主要給大家介紹了關(guān)于Less在Vue中的使用方法,需要的朋友可以參考下
    2023-10-10
  • vue中echarts自動輪播tooltip問題

    vue中echarts自動輪播tooltip問題

    這篇文章主要介紹了vue中echarts自動輪播tooltip問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評論