Vue框架的配置和啟動以及vue項目的目錄介紹(補充:yarn的安裝方式)


 

Vue環境配置

一.安裝vue項目環境

1.安裝vue的腳手架CIL環境

查看官網文檔

 

 

 

 

 

 

但是系統並沒有npm和yarn的環境

 

 

 

"""
node ~~ python   環境
npm ~~ pip     類似於商城

python:c語言編寫,解釋執行python語言的
node:c++語言編寫,解釋執行JavaScript語言的
npm類似於pip,是為node環境安裝額外功能的

"""

2.去node官網下載node

 

 

 

下載好后傻瓜式安裝即可

測試下環境是否添加上

 

 

 

查看npm是否添加上

3.換源:將npm歡迎為cnpm

npm install -g cnpm --registry=https://registry.npm.taobao.org

 

 

 

4.開始安裝腳手架

將官網的方法
npm install -g @vue/cli
替換成下面的
cnpm install -g @vue/cli

 

 

 

下載完成后就會有vue環境了

注:如果第二三四步異常,基本都是由網絡導致的,可以重新執行第二三步,如果一直有問題,可以清理緩存后重復

二.項目創建

開始項目創建

 

 

 

選擇y確定

 

 

 

1.Bable:可以將es[ECAMSCript]6的語法封裝成es5的語法交給瀏覽器
2.TypeScript:ECAMSCript有js和ts,我們一般用js就可以了
3.Progressive Web App (PWA) Support:前台優化機制
4. Router:路由
5. Vuex:全局單例
6. CSS Pre-processors:css和sass是可以寫函數和變量的css預編譯器,我們寫原生css語句,用不上
7.Linter / Formatter:規范
8. Unit Testing:下面兩個測試用的
9.E2E Testing

 

 

 

自己寫項目選擇這三個就可以了,回車進入下一個

選擇y進入下一個

 

 

 

選好后進入下一個

選擇完后就開始安裝項目

在之前的目標文件內開始創建項目文件

補充:

如果以后需要拷貝項目,則有兩種方式:要么全部壓縮成一個文件,(因為上圖第一個文件夾node內有幾萬個文件,下載速度會非常慢,下載速度是以文件為單位下載的)。要么只要下面的方框里面的文件,存入一個新文件夾並命名,最上面的文件通過cmd打開這個新復制的文件的路徑並以下面的方式就會重新下載依賴

cnpm install

 

三.啟動項目

第一種:在cmd終端中通過目標文件夾進行下面的命令

cnpm run serve

 

 

 

 

 

 

通過終端啟動,如果cmd一關閉項目就停了,可以通過pycharm管理

第二種:pycharm

使用pycharm打開目標文件夾,按照下面順序添加環境即可

 

 

 

 

vue項目目錄結構分析

├── v-proj
| ├── node_modules // 當前項目所有依賴,一般不可以移植給其他電腦環境
| ├── public // 當前項目的公有文件
| | ├── favicon.ico // 標簽圖標
| | └── index.html // 當前項目唯一的頁面
| ├── src            
| | ├── assets // 存放靜態資源img、css、js
| | ├── components // 小組件,.vue文件
| | ├── views // 大組件或者頁面組件
| | ├── App.vue // 根組件
| | ├── main.js // 全局腳本文件(項目的入口)
| | ├── router
| | | └── index.js// 路由腳本文件(配置路由 url鏈接 頁面組件的映射關系)
| | └── store
| | | └── index.js// 倉庫腳本文件(vuex插件的配置文件,數據倉庫)
| ├── README.md // 項目的使用說明書,可以自己完善
└── package.json等配置文件

 

vue組件(.vue文件)

"""
注:pycharm安裝vue.js插件,就可以高亮顯示vue文件了

1)一個.vue文件就是一個組件
2)組件都是由三部分組成:html結構、js邏輯、css樣式
  html結構都是在template標簽中,頁面結構有且只有一個根標簽(template一級結構下)
  js邏輯都是在script標簽中,必須設置導出,export default {...}
  css樣式都是在style標簽中,必須設置scoped屬性,是樣式組件化
"""
<template>
   <div class="first-cp">
        <h1>第一個組件</h1>
   </div>
</template>

<script>
   // .vue文件類似於模塊,可以直接相互導入,所以在組件內部要設置導出
   export default {

  }
</script>

<style scoped>
   /* scoped可以使樣式組件化,只在自己內部起作用 */

</style>

 

 

全局腳本文件main.js(項目入口)

"""
1)main.js是項目的入口文件
2)new Vue()就是創建根組件,用render讀取一個.vue文件,$mount渲染替換index.html中的占位
3)項目所依賴的環境,比如:vue環境、路由環境、倉庫環境、第三方環境、自定義環境都是在main.js中完成
"""
<!--不加/表示直接導入是內置包,就是環境依賴里面的文件-->
import Vue from 'vue'
<!--表示導入當前路徑下的App.vue文件-->
import App from './App.vue'
<!--表示導入route-->
import router from './router'
<!--表示導入store-->
import store from './store'
<!--表示打開頁面的時候是否有小提示,false表示沒有,true表示有,需要自己寫-->
Vue.config.productionTip = false

new Vue({
   router,
   store,
   render: h => h(App)
}).$mount(elementOrSelector'#app
改寫
import Vue from 'vue'  // 加載vue環境
import App from './App.vue'  // 加載根組件
import router from './router'  // 加載路由環境
import store from './store'  // 加載數據倉庫環境

Vue.config.productionTip = false;  // tip小提示

import FirstCP from './views/FirstCP'
new Vue({
   el: '#app',  // 等價於上面的.$mount(elementOrSelector'#app')
   router: router,
   store: store,
   render: function (readVueFn) {
       return readVueFn(FirstCP);  // 讀取FirstCP.vue替換index.html中的占位
  }
});

 

App.vue根組件

<!--
1.App.vue是項目的根組件,是唯一由main.js加載渲染的組件,也就是用來替換index.html中的<div id="app"></div>占位符
2.實際開發中只需要下方的五行代碼,然后在不斷完善
3.<router-view/>是前台路由占位標簽,由router插件控制,可以在router的配置中設置
4.<router-view/>會自動根據router.js中配置的路由關系根據前台發出的指定路徑匹配到指定的頁面組件比如Home或者About頁面組件進行渲染
-->
   

<template>
 <div id="app">
   <!--這個是給views中的vue文件占位,末尾的/表示單標簽-->
   <router-view/>
 </div>
</template>


//初始如下,可修改成上面的五行代碼
<template>
 <div id="app">
   <div id="nav">
     <router-link to="/">Home</router-link> |
     <router-link to="/about">About</router-link>
   </div>
   <!--這個是給views中的vue文件占位-->
   <router-view/>
 </div>
</template>

<style>
#app {
 font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
}

#nav {
 padding: 30px;
}

#nav a {
 font-weight: bold;
 color: #2c3e50;
}

#nav a.router-link-exact-active {
 color: #42b983;
}
</style>

如何實現路由跳轉router/index.js

import Vue from 'vue'

// import 別名 from 文件
import VueRouter from 'vu e-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

// 路由配置
// 將加載的Home或者其他頁面文件去替換App.vue文件中的占位符<router-view/>
// redirect實現路由的重定向
 const routes = [
{
   path: '/',   //表示根路由,用戶訪問/或者/home就是加載Home.vue頁面
   name: 'Home',
   component: Home
},
{
     path:'/home',
     redirect:'/'  // 表示路由的重定向,可以指向同一個路由
},  
{
   path: '/about',
   name: 'About',
   // route level code-splitting
   // this generates a separate chunk (about.[hash].js) for this route
   // which is lazy-loaded when the route is visited.
   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
}
]

const router = new VueRouter({
 mode: 'history',   //表示是否允許使用瀏覽器的歷史記錄
 base: process.env.BASE_URL,
 routes
})

export default router

小組間components以及父組件如何加載子組件

一些常用的頁面構成比如導航條等是可以用寫出小組件的,然后一個一個小組間就可以構成頁面組件了

父組件如何加載子組件(三步:導入,注冊,使用)

// 子組件:components/Nav.vue
<template>
   <div class="Nav">
       導航條
   </div>
</template>

<script>
   export default {
       name: "Nav"
  }
</script>

<style scoped>

</style>




//父組件 views/home.vue
<template>
   <div class="home">
       <!--vue項目是支持大小寫並且區分大小寫的-->
       <Nav />   //使用
   </div>
</template>

<script>
   // 父組件如何加載子組件:像views這些頁面組件就是父組件,是由components里面的一個個小組件,就是子組件組裝成的頁面
   //1.導入語法要在export default{}上,@表示src文件夾的絕對路徑
   //2.要在export default{}的括號中用components注冊
   //3.在該組件的模板中就可以渲染子組件了(html文件是區分大小寫的)
   import Nav from '@/components/Nav'  // 導入

   export default {
       // components:{Nav:Nav}
       components:{Nav}    // 注冊
  }
</script>


<style scoped>
  .home {
       width: 100vw;
       height: 100vh;
       background-color: yellow;
  }

</style>

 

 

安裝比npm和cnpm好1000倍的yarn

npm install -g yarn

1、依賴更新

npm install

yarn

2、局部安裝

npm install react

yarn add react

3、全局安裝

npm -g install react

yarn global add react

4、局部刪除

npm uninstall react

yarn remove react

5、全局刪除

npm -g uninstall react

yarn global remove react

6、局部更新

npm update react

yarn upgrade react

7、全局安裝

npm -g update react

yarn global upgrade react

8、查看緩存路徑及清理緩存

yarn cache dir
yarn cache clean

9、啟動項目

yarn run

 

 

 


免責聲明!

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



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