主頁 > .NET開發 > Vue專案框架搭建(不定時更新)

Vue專案框架搭建(不定時更新)

2020-10-21 21:24:26 .NET開發

目錄
  • 創建專案
  • 添加單元測驗
  • eslint代碼檢測
    • 在vue中如何關閉eslint的代碼檢測
  • 添加UI庫Vux
  • 路由 Vue Router
  • 狀態管理 Vuex

創建專案

使用 vue-cli 快速搭建專案結構,關于vue-cli的更多用法,請閱讀官方說明 !vue-cli官方檔案

  • 創建專案
vue create cloud-film-vue
  • 安裝依賴

vscode中,在終端打開檔案夾,然后 npm installyarn

npm install
或
vue add

yarn
  • 啟動專案
yarn run dev

npm run dev

以上是開發環境下,若是生產環境,dev換成build

添加單元測驗

更多jest用法,閱讀官方檔案 !jest官方

  • 根目錄下(與src檔案夾同級目錄)新建 test檔案夾
  • 安裝jest
yarn add jest
或
npm install --save-dev jest
  • 配置jest啟動方式

在package.json中的 “srcipts”:{} 中添加jest的啟動方法,如下:

"scripts": {
    //其他指令
    "test": "jest"  //test指令
  },
  • 在test檔案夾中添加測驗檔案 hello.test.js

測驗檔案必須以xxx.test.js的方式命名,.test.js的檔案,無論在專案中的什么位置,都會被識別為測驗檔案,并在執行測驗單元測驗的時候被執行,

撰寫如下代碼:

const returnHelloWorld = () => {
    return 'hello world'
}
test('輸出hello world ', () => {
    expect(returnHelloWorld()).toBe('hello world')
})
  • 運行測驗用例hello.test.js
yarn test
或
npm run test

執行全部測驗用例,若要執行單個測驗用例,則在 test后指定用例名即可,如:

yarn test hello.test.js
或
npm run test hello.test.js
  • 運行結果
PS E:\Project\CloudFilmVueVersion-Practice\cloudFilm-vue\cloud-film-vue> yarn test
yarn run v1.22.4
$ jest
 PASS  test/hello.test.js
  √ 輸出hello world  (2 ms)
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.001 s
Ran all test suites.
Done in 3.13s.

如上,顯示測驗通過,

eslint代碼檢測

  • 安裝
npm install eslint --save-dev
  • 初始化eslint檔案
./node_modules/.bin/eslint --init

不要懷疑,就是這個指令,

運行指令后,會讓你回答一系列問題,你根據自己的需求認真選擇選項,稍后會根據你的選擇生成對應的代碼檢測規則,

選擇編碼風格時,選 standard,這是我們團隊的規范

  • 運行檢測檔案,檢測代碼格式
./node_modules/.bin/eslint yourfile.js

運行如上命令,會檢查你的代碼,列出格式錯誤的地方,yourfile.js是你要檢測的檔案,如main.js ,就是檢測mian.js的代碼格式問題,

檢測結果:

PS E:\Project\CloudFilmVueVersion-Practice\cloudFilm-vue\cloud-film-vue> ./node_modules/.bin/eslint .eslintrc.json

E:\Project\CloudFilmVueVersion-Practice\cloudFilm-vue\cloud-film-vue\.eslintrc.json
   1:1   error  Expected an assignment or function call and instead saw an expression  no-unused-expressions
   2:1   error  Expected indentation of 2 spaces but found 4                           indent
   2:5   error  Unnecessarily quoted property 'env' found                              quote-props
   2:5   error  Strings must use singlequote                                           quotes
   3:1   error  Expected indentation of 4 spaces but found 8                           indent
   3:9   error  Unnecessarily quoted property 'browser' found                          quote-props
   3:9   error  Strings must use singlequote                                           quotes
   4:1   error  Expected indentation of 4 spaces but found 8                           indent
   4:9   error  Unnecessarily quoted property 'es6' found                              quote-props
   4:9   error  Strings must use singlequote                                           quotes
   5:1   error  Expected indentation of 2 spaces but found 4                           indent
   6:1   error  Expected indentation of 2 spaces but found 4                           indent
   6:5   error  Unnecessarily quoted property 'extends' found                          quote-props
   6:5   error  Strings must use singlequote                                           quotes
   7:1   error  Expected indentation of 4 spaces but found 8                           indent
   7:9   error  Strings must use singlequote                                           quotes
   8:1   error  Expected indentation of 4 spaces but found 8                           indent
   8:9   error  Strings must use singlequote                                           quotes
   9:1   error  Expected indentation of 2 spaces but found 4                           indent
  10:1   error  Expected indentation of 2 spaces but found 4                           indent
  10:5   error  Unnecessarily quoted property 'globals' found                          quote-props
  10:5   error  Strings must use singlequote                                           quotes
  11:1   error  Expected indentation of 4 spaces but found 8                           indent
  11:9   error  Unnecessarily quoted property 'Atomics' found                          quote-props
  11:9   error  Strings must use singlequote                                           quotes
  11:20  error  Strings must use singlequote                                           quotes
  12:1   error  Expected indentation of 4 spaces but found 8                           indent
  12:9   error  Unnecessarily quoted property 'SharedArrayBuffer' found                quote-props
  12:9   error  Strings must use singlequote                                           quotes
  12:30  error  Strings must use singlequote                                           quotes
  13:1   error  Expected indentation of 2 spaces but found 4                           indent
  14:1   error  Expected indentation of 2 spaces but found 4                           indent
  14:5   error  Unnecessarily quoted property 'parserOptions' found                    quote-props
  14:5   error  Strings must use singlequote                                           quotes
  15:1   error  Expected indentation of 4 spaces but found 8                           indent
  15:9   error  Unnecessarily quoted property 'ecmaVersion' found                      quote-props
  15:9   error  Strings must use singlequote                                           quotes
  16:1   error  Expected indentation of 4 spaces but found 8                           indent
  16:9   error  Unnecessarily quoted property 'parser' found                           quote-props
  16:9   error  Strings must use singlequote                                           quotes
  16:19  error  Strings must use singlequote                                           quotes
  17:1   error  Expected indentation of 4 spaces but found 8                           indent
  17:9   error  Unnecessarily quoted property 'sourceType' found                       quote-props
  17:9   error  Strings must use singlequote                                           quotes
  17:23  error  Strings must use singlequote                                           quotes
  18:1   error  Expected indentation of 2 spaces but found 4                           indent
  19:1   error  Expected indentation of 2 spaces but found 4                           indent
  19:5   error  Unnecessarily quoted property 'plugins' found                          quote-props
  19:5   error  Strings must use singlequote                                           quotes
  20:1   error  Expected indentation of 4 spaces but found 8                           indent
  20:9   error  Strings must use singlequote                                           quotes
  21:1   error  Expected indentation of 4 spaces but found 8                           indent
  21:9   error  Strings must use singlequote                                           quotes
  22:1   error  Expected indentation of 2 spaces but found 4                           indent
  23:1   error  Expected indentation of 2 spaces but found 4                           indent
  23:5   error  Unnecessarily quoted property 'rules' found                            quote-props
  23:5   error  Strings must use singlequote                                           quotes
  24:1   error  Expected indentation of 2 spaces but found 4                           indent
  25:2   error  Newline required at end of file but not found                          eol-last

? 59 problems (59 errors, 0 warnings)
  58 errors and 0 warnings potentially fixable with the `--fix` option.

檢測結果:59個問題格式

在vue中如何關閉eslint的代碼檢測

  • 在專案根目錄下增加 vue.config.js,內容為:
// vue.config.js
module.exports = {
    lintOnSave: false
}

添加UI庫Vux

UI庫可以使用Vux,還比較火,更多Vux相關,請查看官方檔案 !Vux官方檔案

  • 安裝vux
npm install vux --save
或
yarn add vux //安裝
yarn upgrade vux // 更新
  • 安裝和配置vux-loader

vux2必須配合vux-loader使用,并配置build/webpack.base.conf.js //新版腳手架可能不用配這個

npm install vux-loader --save-dev
  • 安裝less-loader(用以正確編譯less原始碼)
npm install less less-loader --save-dev
  • 安裝yaml-loader以進行語言檔案讀取
npm install yaml-loader --save-dev
  • 使用

以下是從官方檔案中copy的一段代碼,展示效果如下圖:

<template>
  <div>
    <v-chart :https://www.cnblogs.com/CherishTheYouth/archive/2020/10/21/data="data" prevent-default>
      <v-scale x :tick-count="3" />
      <v-tooltip :show-item-marker="false" show-x-value />
      <v-line />
    </v-chart>
  </div>
</template>

<script>
import { VChart, VTooltip, VLine, VScale } from 'vux'

export default {
  components: {
    VChart,
    VTooltip,
    VLine,
    VScale
  },
  data () {
    return {
      data: [
        { date: '2017-06-05', value: 116 },
        { date: '2017-06-06', value: 129 },
        { date: '2017-06-07', value: 135 },
        { date: '2017-06-08', value: 86 },
        { date: '2017-06-09', value: 73 },
        { date: '2017-06-10', value: 85 },
        { date: '2017-06-11', value: 73 },
        { date: '2017-06-12', value: 68 },
        { date: '2017-06-13', value: 92 },
        { date: '2017-06-14', value: 130 },
        { date: '2017-06-15', value: 245 },
        { date: '2017-06-16', value: 139 },
        { date: '2017-06-17', value: 115 },
        { date: '2017-06-18', value: 111 },
        { date: '2017-06-19', value: 309 },
        { date: '2017-06-20', value: 206 },
        { date: '2017-06-21', value: 137 },
        { date: '2017-06-22', value: 128 },
        { date: '2017-06-23', value: 85 },
        { date: '2017-06-24', value: 94 },
        { date: '2017-06-25', value: 71 },
        { date: '2017-06-26', value: 106 },
        { date: '2017-06-27', value: 84 },
        { date: '2017-06-28', value: 93 },
        { date: '2017-06-29', value: 85 },
        { date: '2017-06-30', value: 73 },
        { date: '2017-07-01', value: 83 },
        { date: '2017-07-02', value: 125 },
        { date: '2017-07-03', value: 107 },
        { date: '2017-07-04', value: 82 },
        { date: '2017-07-05', value: 44 },
        { date: '2017-07-06', value: 72 },
        { date: '2017-07-07', value: 106 },
        { date: '2017-07-08', value: 107 },
        { date: '2017-07-09', value: 66 },
        { date: '2017-07-10', value: 91 },
        { date: '2017-07-11', value: 92 },
        { date: '2017-07-12', value: 113 },
        { date: '2017-07-13', value: 107 },
        { date: '2017-07-14', value: 131 },
        { date: '2017-07-15', value: 111 },
        { date: '2017-07-16', value: 64 },
        { date: '2017-07-17', value: 69 },
        { date: '2017-07-18', value: 88 },
        { date: '2017-07-19', value: 77 },
        { date: '2017-07-20', value: 83 },
        { date: '2017-07-21', value: 111 },
        { date: '2017-07-22', value: 57 },
        { date: '2017-07-23', value: 55 },
        { date: '2017-07-24', value: 60 }
      ]
    }
  }
}
</script>

image-20200930170639098

路由 Vue Router

Vue路由使用 Vue router,具體使用方法詳情,參見 Vue Router中文檔案

  • 安裝
npm install vue-router
  • 使用

具體使用見官方檔案,內容較多,暫不記錄

需要顯式的通過Vue.use()來安裝VueRouter:

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

狀態管理 Vuex

Vue使用Vuex進行狀態管理,其核心思想同redux型別,詳情參見 Vuex中文檔案

  • 安裝
npm install vuex --save
或
yarn add vuex
  • 使用

需要顯式的通過Vue.use()來安裝Vuex:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
  • 在專案中使用

第一步:在src下新建檔案夾 store,在檔案夾下新建4個檔案

+ index.js
+ state.js
+ action.js
+ motation.js

第二步:在 state.js action.js motation.js中分別撰寫對應的代碼:

state.js

const state = {
  name: 'cherish',
  age: 26,
  sex: 'male',
  birthday: '1994-10-22'
}
export default state

action.js

export default {
  // 這里寫對應的代碼  或參照 state.js的寫法也可
}

motation.js

export default {
  // 這里寫對應的代碼  或參照 state.js的寫法也可
}

第三步:在 index.js中引入以上三個檔案,引入Vuex,創建store

import mutation from './mutation'
import action from './action'
import state from './state'
import Vuex from 'vuex'
// Vuex 狀態管理的完整使用模式
Vue.use(Vuex)
const store = new Vuex.Store({
  state,
  mutation,
  action
}) 
export default store

第四步:在組件中提交 motationaction

在組件中使用 this.$store.commit('xxx') 提交 mutation,或者使用 mapMutations 輔助函式將組件中的 methods 映射為 store.commit 呼叫(需要在根節點注入 store

// 提交motation
import { mapMutations } from 'vuex'

export default {
  // ...
  methods: {
    ...mapMutations([
      'increment', // 將 `this.increment()` 映射為 `this.$store.commit('increment')`
      // `mapMutations` 也支持載荷:
      'incrementBy' // 將 `this.incrementBy(amount)` 映射為 `this.$store.commit('incrementBy', amount)`
    ]),
    ...mapMutations({
      add: 'increment' // 將 `this.add()` 映射為 `this.$store.commit('increment')`
    })
  }
}
// 提交action
import { mapActions } from 'vuex'

export default {
  // ...
  methods: {
    ...mapActions([
      'increment', // 將 `this.increment()` 映射為 `this.$store.dispatch('increment')`

      // `mapActions` 也支持載荷:
      'incrementBy' // 將 `this.incrementBy(amount)` 映射為 `this.$store.dispatch('incrementBy', amount)`
    ]),
    ...mapActions({
      add: 'increment' // 將 `this.add()` 映射為 `this.$store.dispatch('increment')`
    })
  }
}

第五步:對于比較復雜的專案,可以將store模塊化,最后在一個總的index.js組合起來

具體實作參見Vuex核心概念——Modules

這有點類似于 React 中的 dva.js 也有命名空間等概念

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/184512.html

標籤:.NET技术

上一篇:快速使用YuebonCore

下一篇:C# .Net 判斷IP地址是否符合某IP段技巧

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more