vuejs_01項目啟動


知識點

1.npm 相關命令

npm list -g --depth=0  查看全局安裝了哪些依賴

項目啟動

npm install vue-cli -g                安裝vue腳手架    

vue init webpack foldername        用webpack構建項目

//npm init -y                         生成package.json文件

npm i                                 下載項目依賴(是根據package.json來安裝)

npm start                             下載完依賴后 運行項目

選擇 git 進行版本控制 

 

 

 

項目目錄

build   構建腳本
config  配置文件
src      寫前端vue項目
   —main.js 整個前端項目的入口,引vue
   —app.vue 根組件

    
static   靜態資源

src->router->index.js 路由路徑設置不區分大小寫

path: '/GoodsList',//路徑這里不區分大小寫

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

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/GoodsList',//路徑這里不區分大小寫
      name: 'GoodsList',
      component: GoodsList
    },
    {
      path: '/Cart',
      name: 'Cart',
      component: Cart
    },
  ]
})

根組件app.vue

vue模板里面只能有一個頂層div

<template>
  <div id="app">
    <!-- <img src="./assets/logo.png"> logo圖標-->
    <!-- router-view 寫的是所有的組件在main.js引入,所有的頁面由router-view進行控制 -->
    <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>

config->index.js 文件夾默認路徑配置

'use strict'
// Template version: 1.2.1
// see http://vuejs-templates.github.io/webpack for documentation.

const path = require('path')

module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static', //默認靜態資源目錄,調用的時候直接用/static/img/1.jpg

    assetsPublicPath: '/',
   proxyTable: {},
// Various Dev Server settings host: 'localhost', // can be overwritten by process.env.HOST port: 8080, // can be overwritten by process.env.HOST, if port is in use, a free one will be determined autoOpenBrowser: false, errorOverlay: true, notifyOnErrors: true, poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions- // Use Eslint Loader? // If true, your code will be linted during bundling and // linting errors and warnings will be shown in the console. useEslint: true, // If true, eslint errors and warnings will also be shown in the error overlay // in the browser. showEslintErrorsInOverlay: false, /** * Source Maps */ // https://webpack.js.org/configuration/devtool/#development devtool: 'eval-source-map', // If you have problems debugging vue-files in devtools, // set this to false - it *may* help // https://vue-loader.vuejs.org/en/options.html#cachebusting cacheBusting: true, // CSS Sourcemaps off by default because relative paths are "buggy" // with this option, according to the CSS-Loader README // (https://github.com/webpack/css-loader#sourcemaps) // In our experience, they generally work as expected, // just be aware of this issue when enabling this option. cssSourceMap: false, }, build: { // Template for index.html index: path.resolve(__dirname, '../dist/index.html'), // Paths assetsRoot: path.resolve(__dirname, '../dist'), assetsSubDirectory: 'static', assetsPublicPath: '/', /** * Source Maps */ productionSourceMap: true, // https://webpack.js.org/configuration/devtool/#production devtool: '#source-map', // Gzip off by default as many popular static hosts such as // Surge or Netlify already gzip all static assets for you. // Before setting to `true`, make sure to: // npm install --save-dev compression-webpack-plugin productionGzip: false, productionGzipExtensions: ['js', 'css'], // Run the build command with an extra argument to // View the bundle analyzer report after build finishes: // `npm run build --report` // Set to `true` or `false` to always turn it on or off bundleAnalyzerReport: process.env.npm_config_report } }

 

開發過程中的技巧:

1.頁面報錯信息:找Error關鍵字

Failed to compile.

./node_modules/vue-loader/lib/template-compiler?{"id":"data-v-21894420","hasScoped":true,"transformToRequire":{"video":"src","source":"src","img":"src","image":"xlink:href"},"buble":{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?type=template&index=0&bustCache!./src/views/GoodsList.vue
Module not found: Error: Can't resolve './../img/1.jpg' in 'F:\24.es6+node.jsl練習\vuejs實戰練習-動腦20171030\vue2-shop-lesson\src\views'
 @ ./node_modules/vue-loader/lib/template-compiler?{"id":"data-v-21894420","hasScoped":true,"transformToRequire":{"video":"src","source":"src","img":"src","image":"xlink:href"},"buble":{"transforms":{}}}!./node_modules/vue-loader/lib/selector.js?type=template&index=0&bustCache!./src/views/GoodsList.vue 238:34-59
 @ ./src/views/GoodsList.vue
 @ ./src/router/index.js
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 webpack/hot/dev-server ./src/main.js

 


免責聲明!

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



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