1. 程式人生 > >Vue 做項目經驗

Vue 做項目經驗

bject true hide 路徑 img 按鈕 輸入 getters item

首先需要知道最基本的東西是:

Vue 項目打包:npm run build

Vue生成在網頁上看的端口:npm run dev

修改端口號的地方在: config文件夾下index.js文件port

改變首頁面在main.js

const routes = [{

path: ‘/‘,

component: gnlb

}

大概在100行

編輯代碼時的一些經驗和坑

Vue相關經驗

1. 引入自己編寫的標簽需要在當前頁面import標簽名from文件路徑,註入進來之後再下面的

components裏面寫入標簽名 然後在template裏面直接使用

例: import table_top from ‘../components/table-top‘

components: {table_top }, 註意components:{},methods:{} 之間要用逗號隔開

2. 定義變量需要在data () {return { }}裏面定義,定義完一個變量後面要加逗號

例: data () {return {c1: ‘‘, c2:‘‘ }}

3. created: function(){ }裏面書寫的是軟件加載完成之前執行的函數

4. mounted: function(){}裏面書寫的是軟件加載完成之後執行的函數

5. methods: {}裏面書寫正常函數 書寫方法:函數名:function(){}, 定義完一個函數後面要加逗號

6. 調用函數或者定義的變量時需要用 this.函數名 或 this.變量名

例: this.get_data(); 或 this.c1

7.axios.post( 接口地址 , 要傳輸的數據) 成功 .then(response => {} 失敗 .catch(function(response) {})

例: axios.post(‘接口地址‘, qs.stringify(data))

  .then(response => {

   //成功回來後做的事情

   })

  .catch(function(response) {

   //失敗回來後做的事情

  });

8. 子級模塊操作函數傳給父級模塊,使父級模塊來操作

例: 父級 <revise @on-hide="onHide"></revise>

子級 <button class="btn" v-on:click="$emit(‘on-hide‘)">確定</button>

9.for循環數組直接出id

  v-for="(循環出的內容,循環出的ID) in要循環的數組"

  例 : v-for="(obj,index) in list"

  v-for循環得到key值

<button v-for=”(gx_item,index) in list”>{{gx_item}}_{{index}}</button>

gx_item是循環list得出來的數據 index就是索引 從零開始

10.watch裏面想監聽一個對象的屬性是 this不能用

  解決辦法:在data裏面return上面給this重新賦值 如 : xthis=this , 然後再在watch裏面操作

11.組件父級想要點擊或者傳對象:

例:

父級:<button1 :btn="{key:1,value:‘錄入‘,click:‘new_dd_btn‘}"></button1>

子級:props:{

btn: {

type: Object,

default:0

},

},

methods:{

btn_click(){

this.$emit(this.btn.click);

}

}

<button @click="btn_click" v-if="btn.key==1">{{btn.value}}</button>

12.父級調用子組件寫內容傳送:

<pan-thumb style="float: left" :image="avatar"> 你的權限:

<span class="pan-info-roles" :key=‘item‘ v-for="item in roles">{{item}}</span>

</pan-thumb>

在pan-thumb組件裏面布好外面之後直接在中間加<slot></slot>這樣父組件之間的字和span標簽都會引入進來

13.父組件調用子組件函數:

父組件:<found ref=‘editUser‘></found >

<div @click="add()">+</div>

methods: {

add(){

this.$refs.editUser.query()

},

}

子組件:methods: {

query (){

console.info(‘111’)

},

}

14.阻止事件冒泡:event.cancelBubble = true; 或者: @click.stope=”aa()”

15.input輸入完按enter觸發按鈕:

<input @keyup.enter=”aaa()”></input>

<button @click=”aaa()”>查詢</button>

Vuex相關經驗

1.Vuex就是提供一個倉庫,倉庫裏面放了很多對象。其中state就是數據源存放地,對應於與一般vue對象裏面的data

例: state: {

Datas: [],

All_datas: {}

},

調用: mutations: {

setTexts (state, x) {

state.Datas[‘list’]=111

},

},

2.mutations: {}裏面書寫的函數是不允許阻塞的 需要很順暢的執行完畢 , 函數和函數之間用逗號隔開 調用這裏面的函數:context.commit(‘函數名‘,參數)

例: mutations: {

setTexts(state, x) {

state.list = x

},

IncBaoNo(state) {

state.All_settings[‘bao_NO‘]++;

state.cur_sminfo=[];

}

調用: context.commit(setTexts, x)

3.actions:{}裏面可以寫任意函數,調用這裏面的函數:this.$store.dispatch(函數名,參數);

例: setcomstate(context, x) {

函數內容

}

調用: this.$store.dispatch(setcomstate, {val: x_val});

4.getter就是把組件中共有的對state 的操作進行了提取,它就相當於 對state 的computed. 所以它會獲得state 作為第一個參數。

例如: getters: {

smdatas: state => {

return state.Datas.slice(0, 20)

}

}

調用: smdatas: (state,getter) => getter. Smdatas

5.state裏面的屬性 再次操作賦值的時候監聽不到 不可以操作

解決辦法: 將對象升維 變成頂層的對象

6.在組件裏面修改state的值

this.$store.state.csmx.isAllActive=1

Element 插件經驗

1.點擊跟隨彈一個小窗 點擊事件必須在el之後

<div>

<el-popover

ref="popover"

placement="bottom"

width="144"

trigger="click">

<div style="text-align: center;cursor:pointer;">

<span @click="set_btn()"><el-row>aaa</el-row></span>

<div class="kong_solid"></div>

<el-row>bbb</el-row>

<div class="kong_solid"></div>

<el-row>ccc</el-row>

</div>

</el-popover>

<img v-popover:popover style="width:30px;" src="../assets/setting.png">

</div>

2. <el-col></el-col>不能使用v-for否則會出現警告 解決辦法 外層加一個div循環div

以後如果還遇見問題了 還會繼續更新。。。

Vue 做項目經驗