当前位置: 首页 > news >正文

使用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

<template>
  <div>
    这是首页内容,展示最新的10篇博客
  </div>
</template>

<script>
  export default {
    name: 'Home',
    data () {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    }
  }
</script>

<style>
</style>

②About.vue

<template>
  <div>
    <!-- 这是关于本站显示的内容区域,本站的发展史... -->
    <router-link to="/AboutMe">关于站长</router-link>
    <router-link to="/AboutWebSite">关于本站</router-link>
    <router-view/>
  </div>
</template>

<script>
  export default {
    name: 'About',
    data () {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    }
  }
</script>

<style>
</style>

3、修改index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Home from '@/components/Home'
import About from '@/components/About'
import AboutWebSite from '@/components/AboutWebSite'
import AboutMe from '@/components/AboutMe'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/HelloWorld',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/Home',
      name: 'Home',
      component: Home
    },
    {
      path: '/About',
      name: 'About',
      component: About,
      children:[
        {
          path: '/AboutMe',
          name: 'AboutMe',
          component: AboutMe
        },
        {
          path: '/AboutWebSite',
          name: 'AboutWebSite',
          component: AboutWebSite
        }
      ]
    }
  ]
})

 

 

三、嵌套路由的使用

1、index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Home from '@/components/Home'
import About from '@/components/About'
import AboutWebSite from '@/components/AboutWebSite'
import AboutMe from '@/components/AboutMe'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/HelloWorld',
      name: 'HelloWorld',
      component: HelloWorld
    },
    {
      path: '/Home',
      name: 'Home',
      component: Home
    },
    {
      path: '/About',
      name: 'About',
      component: About,
      children:[
        {
          path: '/AboutMe',
          name: 'AboutMe',
          component: AboutMe
        },
        {
          path: '/AboutWebSite',
          name: 'AboutWebSite',
          component: AboutWebSite
        }
      ]
    }
  ]
})

2、AboutMe.vue

<template>
  <div>
    站长...
  </div>
</template>

<script>
  export default {
    name: 'AboutMe',
    data () {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    }
  }
</script>

<style>
</style>

 3、AboutWebSite.vue

<template>
  <div>
    网站跌打日志...
  </div>
</template>

<script>
  export default {
    name: 'AboutWebSite',
    data () {
      return {
        msg: 'Welcome to Your Vue.js App'
      }
    }
  }
</script>

<style>
</style>

 

 

相关文章:

  • Windows搭建hexo教程
  • 多线程案例
  • 服务器使用Nginx部署Vue项目
  • 尚医通(二)
  • Java实现登录功能(一)
  • MySQL-插入、更新与删除数据
  • 秋招每日一题T31——二叉搜索子树的最大键值和
  • 天龙八部科举答题问题和答案(全7/8)
  • 【操作系统】内存管理(二)—— 程序运行的基本过程和要求
  • 猿创征文丨赶紧进来!!! 详细介绍C语言指针(九千字完结篇)
  • 2022 CCF BDCI 小样本数据分类任务 baseline
  • 第5/100天 阅读笔记
  • eclipse项目导入教程
  • 如何用SourceInsight阅读大型源码-以Linux内核为例
  • 2.4 赋值
  • [译]如何构建服务器端web组件,为何要构建?
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • Angular Elements 及其运作原理
  • Javascript弹出层-初探
  • leetcode讲解--894. All Possible Full Binary Trees
  • LeetCode刷题——29. Divide Two Integers(Part 1靠自己)
  • MyEclipse 8.0 GA 搭建 Struts2 + Spring2 + Hibernate3 (测试)
  • Object.assign方法不能实现深复制
  • SpiderData 2019年2月25日 DApp数据排行榜
  • Windows Containers 大冒险: 容器网络
  • 跨域
  • 理解在java “”i=i++;”所发生的事情
  • 前端知识点整理(待续)
  • 驱动程序原理
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 使用Gradle第一次构建Java程序
  • 通过几道题目学习二叉搜索树
  • 学习笔记:对象,原型和继承(1)
  • 智能合约开发环境搭建及Hello World合约
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • ​香农与信息论三大定律
  • ${ }的特别功能
  • (9)STL算法之逆转旋转
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (附源码)计算机毕业设计ssm电影分享网站
  • (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致
  • (四)docker:为mysql和java jar运行环境创建同一网络,容器互联
  • (转)VC++中ondraw在什么时候调用的
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • @configuration注解_2w字长文给你讲透了配置类为什么要添加 @Configuration注解
  • [ Linux 长征路第二篇] 基本指令head,tail,date,cal,find,grep,zip,tar,bc,unname
  • [1159]adb判断手机屏幕状态并点亮屏幕
  • [14]内置对象
  • [BZOJ3757] 苹果树
  • [FxCop.设计规则]8. 也许参数类型应该是基类型
  • [G-CS-MR.PS02] 機巧之形2: Ruler Circle
  • [iOS]把16进制(#871f78)颜色转换UIColor
  • [LeetCode]Reverse Linked List II
  • [na]wireshark抓包排错-tcp.flags.reset
  • [NBIoT]NBIoT相关知识