前端工程化(三)---Vue的開發模式


從0開始,構建前后端分離應用

 

導航

前端工程化(一)---工程基礎目錄搭建

前端工程化(二)---webpack配置

前端工程化(三)---Vue的開發模式

 

 

通過前兩部分的總結,項目具備了一個可以運行的前端工程。接下來的工作就是具體的功能開發了,我選擇了Vue作為前端的框架,使用iView作為UI庫。

建議在使用Vue開發之前一定要通讀 Vue官網教程 對Vue中的基本概念及整體的思想有一個基本的認識。最好的教程莫過於官方文檔了,不要上來就各種百度,從一些只言片語中摸索,這樣會少走彎路。

個人感覺使用Vue進行開發,首先要改變以往前端開發中形成的思維模式。對於頁面元素的操作,由原有的dom操作轉換為數據操作。

dom操作的事情,Vue已經替我們干了,我們只需要關注數據就可以了。頁面元素同數據進行了綁定(實際上是Vue模板的元素,只不過Vue的設計擁抱原生的html語法,看上去模板的元素與原生的html元素長得一樣),當數據變化的時候,dom也隨之變化。

 

1、Vue的開發模式:定義一個擴展名為.vue的文件,其中包含三部分內容,模板、js、樣式

<template lang="html">
</template>

<script>
export default {
 
}
</script>

<style lang="css" scoped>
</style>

 

實際的例子:

 1 <template>
 2     <Modal v-model="showFlag" :width="width" :mask-closable="false" :closable="false">
 3         <p slot="header" style="color:#f60;text-align:center">
 4             <Icon :type="customHeader.icon"></Icon>
 5             <span>{{customHeader.title}}</span>
 6         </p>
 7         <div style="text-align:center">
 8             <slot name="content">請定義具體顯示內容</slot>
 9         </div>
10         <div slot="footer">
11             <Button type="text" size="default" :loading="modal_loading" @click="cancel1">取消</Button>
12             <Button type="primary" size="default" :loading="modal_loading" @click="confirm1">確認</Button>
13         </div>
14     </Modal>
15 </template>
16 
17 <script>
18 
19     export default {
20         data: function () {
21             return {
22                 modal_loading: false,
23                 showFlag: false,
24                 onConfirm: 'confirm',
25                 onCancel: 'cancel',
26                 updateFlag:false  //是否為新增操作
27             }
28         },
29         props: {
30             customHeader: {
31                 title: '標題',
32                 icon: 'information-circled'
33             },
34             width: {
35                 type: Number,
36                 default: 500
37             }
38         },
39         methods: {
40             confirm1() {
41                 this.$emit(this.onConfirm,this.updateFlag)
42             },
43             cancel1() {
44                 this.$emit(this.onCancel)
45                 this.showFlag = false
46             },
47             showAdd() {
48                 this.updateFlag = false
49                 this.showFlag = true
50             },
51             showEdit(){
52                 this.updateFlag = true
53                 this.showFlag = true
54             },
55             hide() {
56                 this.showFlag = false
57             }
58         }
59 
60     }
61 </script>
62 
63 <style scoped>
64 
65 </style>
View Code

 


2、定義組件之間共享的數據

在根Vue中定義數據

 1 import Vue from 'vue'
 2 import App from './app.vue'
20 
21 //資源
22 import Data from './assets/data/data.json'
66 new Vue({
67     data:{
68       dict:Data
69     },71     render: h => h(App)
72 }).$mount('#app')

使用:在子組件中,通過this.$root.dict.fileServerPath引用

  1 <template>
 40 </template>
 41 
 42 <script>
 43     export default {
 44         data() { 79         },
 80         methods: {124         },
125         watch: {
126             defaultFiles: function (newV, oldV) {
127                 debugger
128                 newV.forEach(e => {
129  e.url = this.$root.dict.fileServerPath + e.url 130                     e.status = 'finished'
131                     this.$refs.upload.fileList.push(e)
132                 })
133             }
134         },
135         mounted() {
136             this.uploadList = this.$refs.upload.fileList;
137         }
138     }
139 </script>
140 
141 <style scoped>
178 </style>

 

 

3、定義Vue公共組件的方式

方式一

//公共組件
import wolfTotem from './components/common/WolfTotem.js'
//將組件暴露為全局的句柄
window.WT = wolfTotem

 

方式二

import MyLayout from './layout.vue'

const _layout = {
  install:function(Vue){
    Vue.component('WtLayout',MyLayout)
  }
}

export default _layout
//自定義組件
import WtLayout from './components/layout/layout.js'

//織入
Vue.use(WtLayout)

方式三

import HttpUtil from './assets/js/httpUtil.js'
Vue.prototype.$http = HttpUtil

 

 

 前端的開發圍繞着上面的方式進行

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM