• 使用vue-cli搭建SPA项目


    目录

    一、如何使用vue-cli搭建SPA项目

    1、构建前提

    2.利用Vue-cli来构建spa项目

    二、如何在spa项目中使用路由

    1、修改端口号

    2、自定义vue文件

    3、修改index.js

    三、嵌套路由的使用

    1、index.js

    2、AboutMe.vue

     3、AboutWebSite.vue


    一、如何使用vue-cli搭建SPA项目

    1、构建前提

    nodeJS环境已经搭建完毕 

    2.利用Vue-cli来构建spa项目

    ①什么是Vue-cli

    vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板,创建命令如下:
    vue init webpack xxx
    注1:xxx 为自己创建项目的名称
    注2:必须先安装vue,vue-cli,webpack,node等一些必要的环境 

    ②安装vue-cli 

    npm install -g vue-cli
    npm install webpack -g

    ③使用脚手架vue-cli(2.X版)来构建项目 

    使用脚手架vue-cli(2.X版)来构建项目
    步骤一:使用脚手架创建项目骨架
    此步骤可理解成:使用eclipse创建一个maven的web项目
    cmd #打开命令窗口
    d: #切换到d盘
    cd d:\temp #进入d:\temp目录
    vue init webpack spa1 #此命令用于创建SPA项目,它会在当前目录生成一个以“spa1”命名的文件夹
    “一问一答”模式
    1.Project name:项目名,默认是输入时的那个名称spa1,直接回车
    2.Project description:项目描述,直接回车
    3.Author:作者,随便填或直接回车
    4.Vue build:选择题,一般选第一个
    4.1Runtime + Compiler: recommended for most users//运行加编译,官方推荐,就选它了
    4.2Runtime-only: about 6KB lighter min+gzip, but templates (or any Vue-specific HTML) are ONLY allowed in .vue files
    - render functions are required elsewhere//仅运行时,已经有推荐了就选择第一个了
    5.Install vue-router:是否需要vue-router,Y选择使用,这样生成好的项目就会有相关的路由配置文件
    6.Use ESLint to lint your code:是否用ESLint来限制你的代码错误和风格。N 新手就不用了,但实际项目中一般都会使用,这样多人开发也能达到一致的语法
    7.Set up unit tests:是否安装单元测试 N
    8.Setup e2e tests with Nightwatch?:是否安装e2e测试 N
    9.Should we run npm install for you after the project has been created? (recommended) (Use arrow keys)
    Yes, use NPM
    Yes, use Yarn
    No, I will handle that myself //选择题:选第一项“Yes, use NPM”是否使用npm install安装依赖
    全部选择好回车就进行了生成项目,出现如下内容表示项目创建完成
    Project initialization finished!

     

    二、如何在spa项目中使用路由

    1、修改端口号

     config --> index.js
    dev: {
    // Paths
    assetsSubDirectory: ‘static’,
    assetsPublicPath: ‘/’,
    proxyTable: {},
    host: ‘localhost’,
    port: 8083, // 在这里修改端口号
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    },

    2、自定义vue文件

    ①Home.vue

    1. <template>
    2. <div>
    3. 这是首页内容,展示最新的10篇博客
    4. div>
    5. template>
    6. <script>
    7. export default {
    8. name: 'Home',
    9. data () {
    10. return {
    11. msg: 'Welcome to Your Vue.js App'
    12. }
    13. }
    14. }
    15. script>
    16. <style>
    17. style>

    ②About.vue

    1. <template>
    2. <div>
    3. <router-link to="/AboutMe">关于站长router-link>
    4. <router-link to="/AboutWebSite">关于本站router-link>
    5. <router-view/>
    6. div>
    7. template>
    8. <script>
    9. export default {
    10. name: 'About',
    11. data () {
    12. return {
    13. msg: 'Welcome to Your Vue.js App'
    14. }
    15. }
    16. }
    17. script>
    18. <style>
    19. style>

    3、修改index.js

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. import HelloWorld from '@/components/HelloWorld'
    4. import Home from '@/components/Home'
    5. import About from '@/components/About'
    6. import AboutWebSite from '@/components/AboutWebSite'
    7. import AboutMe from '@/components/AboutMe'
    8. Vue.use(Router)
    9. export default new Router({
    10. routes: [
    11. {
    12. path: '/HelloWorld',
    13. name: 'HelloWorld',
    14. component: HelloWorld
    15. },
    16. {
    17. path: '/Home',
    18. name: 'Home',
    19. component: Home
    20. },
    21. {
    22. path: '/About',
    23. name: 'About',
    24. component: About,
    25. children:[
    26. {
    27. path: '/AboutMe',
    28. name: 'AboutMe',
    29. component: AboutMe
    30. },
    31. {
    32. path: '/AboutWebSite',
    33. name: 'AboutWebSite',
    34. component: AboutWebSite
    35. }
    36. ]
    37. }
    38. ]
    39. })

     

     

    三、嵌套路由的使用

    1、index.js

    1. import Vue from 'vue'
    2. import Router from 'vue-router'
    3. import HelloWorld from '@/components/HelloWorld'
    4. import Home from '@/components/Home'
    5. import About from '@/components/About'
    6. import AboutWebSite from '@/components/AboutWebSite'
    7. import AboutMe from '@/components/AboutMe'
    8. Vue.use(Router)
    9. export default new Router({
    10. routes: [
    11. {
    12. path: '/HelloWorld',
    13. name: 'HelloWorld',
    14. component: HelloWorld
    15. },
    16. {
    17. path: '/Home',
    18. name: 'Home',
    19. component: Home
    20. },
    21. {
    22. path: '/About',
    23. name: 'About',
    24. component: About,
    25. children:[
    26. {
    27. path: '/AboutMe',
    28. name: 'AboutMe',
    29. component: AboutMe
    30. },
    31. {
    32. path: '/AboutWebSite',
    33. name: 'AboutWebSite',
    34. component: AboutWebSite
    35. }
    36. ]
    37. }
    38. ]
    39. })

    2、AboutMe.vue

    1. <template>
    2. <div>
    3. 站长...
    4. div>
    5. template>
    6. <script>
    7. export default {
    8. name: 'AboutMe',
    9. data () {
    10. return {
    11. msg: 'Welcome to Your Vue.js App'
    12. }
    13. }
    14. }
    15. script>
    16. <style>
    17. style>

     3、AboutWebSite.vue

    1. <template>
    2. <div>
    3. 网站跌打日志...
    4. div>
    5. template>
    6. <script>
    7. export default {
    8. name: 'AboutWebSite',
    9. data () {
    10. return {
    11. msg: 'Welcome to Your Vue.js App'
    12. }
    13. }
    14. }
    15. script>
    16. <style>
    17. style>

     

     

  • 相关阅读:
    Python:用pythonping处理ping
    macOS Big Sur(macos11版本)
    【pen200-lab】10.11.1.222
    CSS选择器分类( 通配符、标签选择器、id选择器、类选择器)
    【linux基础】linux中文件权限的含义并修改
    C++数据结构之链表栈
    如何驱动直流电机H桥驱动笔记
    带看123456
    《 合 成 大 西 瓜 》 重 制 版 !( 联 机 版 在 做 了 )
    Hadoop 基础文件上传下载 警告:WARN util.NativeCodeLoader解决 MapReduce入门
  • 原文地址:https://blog.csdn.net/qq_44247968/article/details/126802093