vue-cli腳手架(框架)

1、建立vue項目

npm install vue-cli -g   #-g全局
(sudo)npm install vue-cli -g   #mac筆記本

vue-init webpack myvue  #項目的名字
cd muvue
npm install
npm run dev

 

? Project name myvue              --項目名
? Project description A Vue.js project
? Author Mark-IT <269773075@qq.com>    --做者,默認git帳戶名
? Vue build standalone                   --vue構建
? Install vue-router? Yes                --安裝vue路由
? Use ESLint to lint your code? Yes      --使用eslint連接代碼
? Pick an ESLint preset Standard
? Set up unit tests No                 --設置單元測試
? Pick a test runner jest                --選擇一個測試運行
? Setup e2e tests with Nightwatch? Yes  --用nightwatch設置e2e測試
? Should we run `npm install` for you after the project has been created? (recommended) npm

   vue-cli · Generated "myvue".

不過中途有可能會由於是國外服務器的緣故有些包安裝錯誤,錯誤的時候繼續執行npm installcss

 好了以後執行上邊黃色指令運行項目html

 

 

修改爲true後,執行npm run dev,出現下面這樣的圖就說明成功了vue

 

雖然完成了,可是速度太慢了,建議使用淘寶鏡像安裝,快!node

首先咱們須要下載npm,由於我已經提早安裝了node.js,安裝包裏面集成了npm,而後咱們就能夠利用npm命令從獲取淘寶鏡像的cnpm了。 
1.打開命令行窗口,輸入
npm install -g cnpm --registry=https://registry.npm.taobao.org
獲取到cnpm之後,咱們須要升級一下,輸入下面的命令
cnpm install cnpm -g
由於安裝Vue須要npm的版本大於3.0.0,因此咱們要升級一下,

而後咱們輸入下面的命令,安裝vue
cnpm install vue
接下來安裝vue-cli
cnpm install --global vue-cli
此時環境就搭建好了。
2.接下來咱們須要指定一個目錄存放咱們的項目,能夠選擇任意路徑,肯定好路徑後輸入下面的命令
vue init webpack "項目名稱"
3.成功之後,咱們進入項目所在目錄
cd "項目所在文件夾"
而後依次輸入下面的命令
cnpm install 
cnpm run dev

 

2、目錄結構的說明

簡單介紹:webpack

build目錄是一些webpack的文件,配置參數什麼的,通常不用動ios

config是vue項目的基本配置文件 git

node_modules是項目中安裝的依賴模塊 github

src源碼文件夾,基本上文件都應該放在這裏。web

  assets 資源文件夾,裏面放一些靜態資源 
  components這裏放的都是各個組件文件 
  App.vue App.vue組件 
  main.js入口文件 vue-router

static生成好的文件會放在這個目錄下。

test測試文件夾,測試都寫在這裏 

.babelrc babel編譯參數,vue開發須要babel編譯 
.editorconfig 編輯器的配置文件 
.gitignore 用來過濾一些版本控制的文件,好比node_modules文件夾 ,通常在githubpush文件的時候須要用到
index.html 主頁 
package.json 項目文件,記載着一些命令和依賴還有簡要的項目描述信息 
README.md 介紹本身這個項目的,隨便寫,讓別人看得懂就行,看不懂就算了

詳細介紹:

1、package.json
  package.json文件是項目的配置文件,除了一些項目的基本配置信息,還有幾個地方須要重點記憶
    dependcies:項目發佈時的依賴
    devDependencies:項目開發時的依賴
    scripts:編譯項目時的一些命令
2、.babellrc文件:
  .babelrc文件定義了ES6的轉碼規則,基於ES6編寫的js代碼在編譯時都會被babel轉碼器轉換爲ES5代碼
    { 
      "presets": ["es2015", "stage-2"],
       "plugins": ["transform-runtime"],
       "comments": false 
    }
3、index.html
  主頁咱們能夠像平時普通的html文件同樣引入文件和書寫基本信息,添加meta標籤等。
4、main.js
  這裏是入口文件,能夠引入一些插件或者是靜態資源,引入以前要肯定已經安裝了該插件,記錄能夠在package.json文件中查看
  /*引入Vue框架*/ 
  import Vue from 'vue' 
  /*引入資源請求插件*/ 
  import VueResource from 'vue-resource' 
  /*重置樣式*/ 
  import "assets/css/base.css" 
  /*基本JS*/   
  import "assets/js/common.js" 
  /*引入路由設置*/ 
  import "./routers.js"   
  /*使用VueResource插件*/ 
  Vue.use(VueResource)
5、App.vue
  這個是一個標準的vue組件,包含三個部分,一個是模板,一個是script,一個是樣式

 

http://www.jb51.net/article/111658.htm

 

3、import和require的區別

import必定要放在文件頂部,他至關於一個指針引用了文件,並無吧文件包含進來,須要調用文件時才引入
require能夠把文件放在任何位置,他是把文件直接包含進來

 

4、設置文件路徑的流程

1)創建組件(.vue的文件)
2)配置路由(index.js文件中配置)
3)<router-link></router-link>
4)<router-view></router-view>
5)import 包名 from "組件路徑"
6)comonents進行註冊

 

5、實現異步加載

//異步
vue-resource:實現異步加載數據(已經棄用)
axios:實現異步加載數據
npm install axios --save-dev
npm install vue-axios --save-dev

 

6、VUE的生命週期

一、定義vue對象並實例化
二、執行created函數
三、編譯模板,只會編譯template的模板
四、把HTML元素渲染到頁面當中
五、執行mounted函數,(加載)至關於onload
六、若是有元素的更新,就執行update函數
七、銷燬實例

 

 

7、仿抽屜網站

ALL.vue

<template>
  <div class='box'>
    <ul>
      <li v-for='item in arr'>
        <div class='p1'>
          <router-link :to="{path:'/detail',query:{ids:item.id}}">{{item.content}} </router-link>
        </div>
        <div class="p2">
          <img :src="item.imgUrl">
        </div>
      </li>

    </ul>

  </div>
</template>

<script>
  export default {
    name: 'HelloWorld',
    data () {
      return {
        arr: []
      }
    },
    mounted () {
      var url = '../../static/news.json'
      var self=this;
      this.$axios.get(url)
        .then(function (response) {
          console.log(response.data.result.data);
          self.arr = response.data.result.data;
        })
        .catch(function (error) {
          console.log(error);
        })
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  h1, h2 {
    font-weight: normal;
  }

  ul {
    list-style-type: none;
    padding: 0;
  }

  li {
    display: inline-block;
    margin: 0 10px;
  }

  a {
    color: #42b983;
  }
.box{
  width: 980px;
}
.p1{
  float:left;
  width:780px;
}
 img{
  float:right;
 }
</style>
View Code

Detail.vue

<template>
  <div class="box">
    <h1>我是詳細頁面{{id}}</h1>
    <ul>
      <li>
        <div class="p1">
          {{obj.content}}
        </div>
        <div class="p2">
          <img :src="obj.imgUrl">
        </div>

      </li>
    </ul>
  </div>
</template>

<script>
  export default {
    name: 'Detail',
    data () {
      return {
          obj:{} ,
          id:this.$route.query.ids
      }
    },
    mounted(){
      var url = "../../static/news.json"
      var self =this;
      this.$axios.get(url,{
          params:{id:this.id}
      })
        .then(function (response) {
          //console.log(response.data.result.data);
          self.obj = response.data.result.data[0];
        })
        .catch(function (error) {
          console.log(error);
        })
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  h1, h2 {
    font-weight: normal;
  }

  ul {
    list-style-type: none;
    padding: 0;
  }

  li {
    display: inline-block;
    margin: 0 10px;
  }

  a {
    color: #42b983;
  }
  .box{
    width: 980px;
  }

  .p1{
    float:left;
    width:700px;
  }
  .p2{
    float:right;
  }
</style>
View Code

DUANZI.vue

<template>
  <div>
    <h1> 我是段子手</h1>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {

    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
View Code

NaveList.vue

<template>
  <div>
    <router-link to="/">首頁</router-link>
    <router-link to="/news">新聞</router-link>
    <router-link to="/duanzi">段子</router-link>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {

    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
View Code

NEWS.vue

<template>
  <div>
    <h1> 我是新聞</h1>

  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {

    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>
View Code

index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import ALL from '@/components/All'
import NEWS from '@/components/NEWS'
import DUANZI from '@/components/duanzi'
import Detail from '@/components/Detail'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/hw',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/',
      name: 'ALL',
      component: ALL
    },
    {
      path: '/news',
      name: 'NEWS',
      component: NEWS
    },
    {
      path: '/duanzi',
      name: 'duanzi',
      component: DUANZI
    },
    {
      path: '/detail',
      name: 'Detail',
      component: Detail
    },


  ]
})
View Code
import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import ALL from '@/components/All'
import NEWS from '@/components/NEWS'
import DUANZI from '@/components/duanzi'
import Detail from '@/components/Detail'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/hw',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/',
      name: 'ALL',
      component: ALL
    },
    {
      path: '/news',
      name: 'NEWS',
      component: NEWS
    },
    {
      path: '/duanzi',
      name: 'duanzi',
      component: DUANZI
    },
    {
      path: '/detail',
      name: 'Detail',
      component: Detail
    },


  ],
  mode:"history" //去掉地址欄中的  #
})
去掉地址欄中的#

App.vue

<template>
  <div id="app">
  <NavList></NavList>
    <router-view></router-view>
  </div>
</template>

<script>
import NavList from './components/NavList'
export default {
  name: 'App',
  components: {NavList}
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
View Code

main.js 

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import VueAxios from 'vue-axios'

// Vue.prototype.$axios = axios; //方式一

Vue.use(VueAxios,axios); //方式二,註冊axios
//Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
View Code

 

 項目須要注意的問題

 問題一:在手動執行webpack app/a.js public ndle.js打包時出錯的解決方法須要修改成: require("style-loader!css-loader!./style.css")
 問題2:腳手架生成項目後,運行 npm run dev啓動項目後,
             若是想把地址欄中的  #去掉,如:http://localhost:8080/#/news,須要在
             router文件夾下的index.js文件中,加入 mode: "history"

問題三:引入axios的2種方法:
    須要在main.js中進行設置:這2種方法均可以,但引用順序不能翻轉。

相關文章
相關標籤/搜索