Vue的學習--怎麼在vue-cli中寫網頁

  vue.min.js和vue-cli的區別和聯繫我如今仍是沒有太清楚,大概是還沒搞清楚export default和new Vue的區別,先淺淺記錄一下怎麼「用vue-cli來寫網頁」。html

  「vue-cli是一個能夠快速搭建大型單頁應用的官方命令行工具。 」在討論這個問題前,先把項目的目錄放出來(環境的配置和項目的建立在上一篇):vue

  

 

            • build 操做文件,經過npm run * 可執行其中的文件
            • config 配置文件
            • src 資源文件,全部的圖片、組件都在這個文件夾內

  開發階段咱們關注config的index.js和src文件夾,index.js文件包含了簡單的環境配置,先來看看src和index.html,當咱們在項目根目錄使用npm run dev將初始的helloworld跑起來,而後訪問127.0.0.1:8080,界面以下:jquery

 

 

一、index.html+srcwebpack

  所謂的單頁應用,大概由於整個vue項目中只存在一個html文件吧,所謂頁面跳轉,作的只是頁內的界面的更新。對於整個項目,index.html是咱們所見的內容,src\App.vue作的是全部頁面共有的部分,真正編輯頁面的是src\components中的vue文件,定義路由跳轉(將components嵌入App.vue)在src\router,圖片等資源文件放在src\assets,與後端交接數據經過的是vue界面中接口訪問獲取數據,如axios。ios

  具體的以下所示web

  <1>index.html中咱們只能夠看到一個'#app'的div,做爲項目網頁的主體,咱們能夠在index.html中引入全局的js文件vue-router

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>vue-test</title>
    <script src="../static/lib/jquery-3.3.1.min.js"></script><!--能夠在這裏引入全局的js文件-->
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

  <2>src\mian.js能夠看到爲index.html中'#app'綁定App.vue的操做vue-cli

// 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'

Vue.config.productionTip = false

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

 

   <3>App.vue中能夠編寫全部網頁公共的部分,如導航欄、頁頭頁尾等等。其中的<router-view/>則是引入的子組件的位置,具體的聯繫以後再說。npm

<template>
  <div id="app">
    <img src="./assets/logo.png"><!--全部網頁共有-->
    <router-view/><!--組件位置-->
  </div>
</template>

<script>
export default {
  name: 'App'
}
</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>

 

  <4>components中放的是子組件.vue文件,如示例文件helloworld.vue,<temple></temple>元素中以單獨一個div開始定義頁面,在export default中綁定數據和頁面。axios

  <5>那麼子組件是如何和App.vue聯繫起來的呢?經過router文件夾下的index.js聯繫的,以下圖。

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    }
  ]
})

 參考:

https://www.jianshu.com/p/09fcc8173a49

https://blog.csdn.net/wulala_hei/article/details/80488674

相關文章
相關標籤/搜索