1. 程式人生 > >vue腳手架搭建之單檔案元件的開發

vue腳手架搭建之單檔案元件的開發

由於一個完整的專案包含許多的檢視,元件全部寫在一個html頁面中也不利於後期維護。所以我們需要通過腳手架進行專案工程化來構建我們的專案,就不再使用html來構建了,也不在引入vue.js檔案了。全部換成單檔案元件+webpack+vue的腳手架進行專案的構建。

首先,需要全域性安裝vue腳手架(全域性安裝,只需一次),方法是: npm  i  vue-cli   -global

然後就可以構建專案了,vue提供了兩種模板:webpack的模板、webpack-simple的模板。

  1. 官方模版 vue init webpack my-project    (tips:程式碼語法檢查較麻煩,適合新手使用)

  2. 推薦  vue init webpack-simple my-project   (tips:適合熟練vue腳手架專案的人用)

用這個模板構建專案的前提是已經配置過vue和webpack以及vue腳手架的環境,即 npm install -g vue全域性安裝、npm i webpack -g全域性安裝、 npm install -g vue-cli腳手架全域性安裝。使用指令模板之後,根據提示進行依賴安裝。

  • 專案啟動      npm run dev
  • 專案打包      npm  run build
  • 單檔案元件     元件  css擁有作用域,scoped屬性可規定當前css只作用於自己的元件,不影響其他

以一個移動端小專案來代入-------------

假設vue和webpack環境已配好,新建一個目錄並調出小黑窗-->npm  i vue-cli -g(安裝過就不用再裝)--->vue init webpack pro--->cd pro--->npm run dev執行即可,會啟動8080埠,瀏覽器開啟localhost:8080,若佔用會預設加1即8081。

專案構建好之後就可以根據需求來修改配置了。下面用路由來實現三個頁面的跳轉:

  1. //APP.vue

  2. <template>

  3. <div id="app">

  4. <header>{{str}}</header>

  5. <section>

  6. <router-view @toparent="getdata"></router-view>

  7. </section>

  8. <footer>

  9. <router-link to="/home" tag="span">首頁</router-link>

  10. <router-link to="/about" tag="span">關於</router-link>

  11. <router-link to="/other" tag="span">其他</router-link>

  12. </footer>

  13. </div>

  14. </template>

  15. <script>

  16. export default {

  17. name: 'App',

  18. data:function(){

  19. return{

  20. str:"首頁"

  21. }

  22. },

  23. methods:{

  24. getdata(msg){

  25. this.str=msg

  26. }

  27. }

  28. }

  29. </script>

  30. <style>

  31. *{

  32. margin: 0;

  33. padding: 0;

  34. }

  35. .router-link-active{

  36. color: yellow;

  37. }

  38. #app {

  39. height: 100Vh;

  40. display: flex;

  41. flex-direction: column;

  42. }

  43. header,footer{

  44. height: 40px;

  45. background: blue;

  46. color: #fff;

  47. text-align: center;

  48. line-height: 40px;

  49. }

  50. section{

  51. flex: 1;

  52. }

  53. footer span{

  54. float: left;

  55. width: 33.3%;

  56. }

  57. </style>

注:這個單檔案元件在main.js已在vue例項化中掛載,index.html頁面引入即可展示。在這個元件裡面可以通過router-link做一個導航路由,去寫一下導航路由連線的元件(在專案的src資料夾的components目錄下建Home、About、Other元件)

  1. //Home.vue

  2. <template>

  3. <div>

  4. <h1>首頁</h1>

  5. </div>

  6. </template>

  7. <script>

  8. export default{

  9. name:"Home",

  10. data(){

  11. return{

  12. str:"首頁"

  13. }

  14. },

  15. mounted(){

  16. this.$emit("toparent",this.str)

  17. }

  18. }

  19. </script>

  20. <style>

  21. </style>

  1. //Other.vue

  2. <template>

  3. <div>

  4. <h1>其他</h1>

  5. </div>

  6. </template>

  7. <script>

  8. export default{

  9. name:"Other",

  10. data:function(){

  11. return{

  12. str:"其他"

  13. }

  14. },

  15. mounted(){

  16. this.$emit("toparent",this.str)

  17. }

  18. }

  19. </script>

  20. <style>

  21. </style>

  1. //About.vue

  2. <template>

  3. <div>

  4. <h1>關於</h1>

  5. </div>

  6. </template>

  7. <script>

  8. export default{

  9. name:"About",

  10. data(){

  11. return{

  12. str:"關於"

  13. }

  14. },

  15. mounted(){

  16. this.$emit("toparent",this.str)

  17. }

  18. }

  19. </script>

  20. <style>

  21. </style>

然後在router目錄下配置一下路由規則

  1. //index.js

  2. import Vue from 'vue'

  3. import Router from 'vue-router'

  4. import HelloWorld from '@/components/HelloWorld'

  5. import Home from '../components/Home'

  6. import About from '../components/About'

  7. import Other from '../components/Other'

  8. Vue.use(Router)

  9. export default new Router({

  10. routes: [

  11. {

  12. path: '/home',

  13. component: Home

  14. },

  15. {

  16. path: '/about',

  17. component: About

  18. },

  19. {

  20. path: '/other',

  21. component: Other

  22. },

  23. {

  24. path: '*',

  25. redirect:"/home"

  26. }

  27. ]

  28. })

注:上面程式碼有用到子元件向父元件傳值,用的是自定義繫結事件@toparent="getdata",子元件在掛載的時候通過this.$emit("toparent",this.str)將資料傳送給父元件,父元件通過事件getdata(msg){console.log(msg)}來接收,msg就是接收的資料。

下面就是用axios來請求資料的使用,需要安裝依賴npm i axios -S,哪個檔案使用就在哪個檔案中引入,引入方式是import axios from "axios".上程式碼:

  1. //Home.vue

  2. <template>

  3. <div>

  4. <h1>首頁</h1>

  5. <ul>

  6. <li v-for="item in list"><router-link :to="'/detail/'+item.pid" tag="span">{{item.pname}}</router-link></li>

  7. </ul>

  8. </div>

  9. </template>

  10. <script>

  11. import axios from "axios"

  12. export default{

  13. name:"Home",

  14. data(){

  15. return{

  16. str:"首頁",

  17. list:[]

  18. }

  19. },

  20. mounted(){

  21. this.$emit("toparent",this.str)

  22. var _this = this

  23. axios({

  24. url:"http://jx.xuzhixiang.top/ap/api/productlist.php"

  25. }).then(function(data){

  26. _this.list = data.data.data

  27. })

  28. }

  29. }

  30. </script>

  31. <style>

  32. </style>

在上面Home元件裡面請求資料展示在頁面上,然後通過params傳參到詳情頁面。下面是詳情頁面的元件:

  1. //Detail.vue

  2. <template>

  3. <div>

  4. <h1>詳情</h1>

  5. <ul>

  6. <li v-for="item in arr">

  7. <img :src="item.imgSrc"/>

  8. <span >{{item.desC}}</span>

  9. </li>

  10. </ul>

  11. </div>

  12. </template>

  13. <script>

  14. import axios from "axios"

  15. export default{

  16. name:"Detail",

  17. data(){

  18. return{

  19. str:"詳情",

  20. arr:[],

  21. imgsrc:"",

  22. desc:""

  23. }

  24. },

  25. mounted(){

  26. this.$emit("toparent",this.str);

  27. var _this = this;

  28. axios({

  29. url:"http://jx.xuzhixiang.top/ap/api/detail.php",

  30. params:{id:_this.$route.params.id}

  31. }).then(function(data){

  32. _this.imgsrc = data.data.data.pimg;

  33. _this.desc = data.data.data.pdesc;

  34. _this.arr.push({imgSrc:_this.imgsrc,desC:_this.desc});

  35. })

  36. }

  37. }

  38. </script>

  39. <style scoped="">

  40. ul li{

  41. width: 300px;

  42. height: 480px;

  43. background: #CCCC99;

  44. float: left;

  45. margin: 10px;

  46. }

  47. img{

  48. display: block;

  49. width: 260px;

  50. height: 300px;

  51. margin:10px 0 0 20px;

  52. }

  53. span{

  54. display: block;

  55. margin:20px 0 0 20px;

  56. }

  57. </style>

index.js中配置一下路由規則

  1. //index.js

  2. import Vue from 'vue'

  3. import Router from 'vue-router'

  4. import HelloWorld from '@/components/HelloWorld'

  5. import Home from '../components/Home'

  6. import About from '../components/About'

  7. import Other from '../components/Other'

  8. import Detail from '../components/Detail'

  9. Vue.use(Router)

  10. export default new Router({

  11. routes: [

  12. {

  13. path: '/home',

  14. component: Home

  15. },

  16. {

  17. path: '/about',

  18. component: About

  19. },

  20. {

  21. path: '/other',

  22. component: Other

  23. },

  24. {

  25. path: '/detail/:id',

  26. component: Detail

  27. },

  28. {

  29. path: '*',

  30. redirect:"/home"

  31. }

  32. ]

  33. })

對傳遞資料的處理寫在詳情元件上面。

引入icon圖示的方法,main.js裡面匯入檔案

import './icon/iconfont.css'

和vue搭建比較好的框架有Element(適合pc端)餓了嗎餓了嗎     Mintui(適合移動端的UI框架)mint-ui要翻牆要翻牆

另外,vue裡面返回上一級的操作是:$router.back(-1),這個經常會用到。