使用vue-cli来配置webpack,webpack是一个打包工具,使程序模块化

全局安装vue-cli:

npm install -g vue-cli

安装好后,使用vue-cli脚手架配置webpack:

vue init webpack lanspa

lanspa 为项目名称,ESLint是一个QA工具,用来避免低级错误和统一代码的风格,我选了no. 安装vue-router 允许我们在 页面/路由 之间进行切换,而不会 刷新/重新 加载页面

然后

  cd spa
npm install
// 运行开发服务
npm run dev

即可看到页面。修改页面默认的功能:

打开src里的main.js,可以看到为:

// 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,
template: '<App/>',
components: { App }
})

替换为:

// 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 the vue instance
import Vue from 'vue'
//import the App component
import App from './App'
//import the vue router
import VueRouter from 'vue-router'
//tell vue to use the router
Vue.use(VueRouter)
/* eslint-disable no-new */
//import the hello component
import Hello from './components/Hello'
//import the about component
import About from './components/About'
//define your routes
const routes = [
//route for the home route of the webpage
{ path: '/', component: Hello },
//route for the about route of the webpage
{ path: '/about', component: About }
] // Create the router instance and pass the `routes` option
// You can pass in additional options here, but let's
// keep it simple for now.
const router = new VueRouter({//创建路由
routes, // short for routes: routes
mode: 'history'//以防止我们的 URL 中包含 # 标记
})
//instatinat the vue instance
new Vue({
//define the selector for the root component
el: '#app',
//pass the template to the root component
template: '<App/>',
//declare components that the root component can access
components: { App },
//pass in the router to the vue instance
router
}).$mount('#app')//mount the router on the app

打开App.vue文件,看到

<template>
<div id="app">
<img src="./assets/logo.png">
<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>

替换为:

<template>
<div id="app">
<!-- the router outlet, where all matched components would ber viewed -->
<router-link v-bind:to="'/'">Home</router-link>
<!-- 为我们创建两个锚点标签,并动态路由,使页面不需要重新加载-->
<router-link v-bind:to="'/about'">About</router-link>
<router-view></router-view>
</div>
</template> <script>
export default {
name: 'app'
}
</script>
<!-- styling for the component -->
<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>

两者主要区别为:1 router-view 标签被放置在了 template 内,用于渲染视图。

2 删除 hello 组件的 import 语句。

3 在 script 标签中删除了组件代码块

此时重新加载可看到新页面。

定义一个新路由的方法:

1 在 src/components 文件夹内创建一个名为 About.vue 的文件,hello.vue文件也是一样的:

<template>
<div id="about">
blabla bla bla hahahah
</div>
</template> <script>
export default {
name: 'about'
}
</script>
<!-- styling for the component -->
<style>
#about {
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>

要渲染about.vue,需要设置路由,即前文main.js中的

import Hello from './components/Hello'
//import the about component
import About from './components/About'
//define your routes
const routes = [
//route for the home route of the webpage
{ path: '/', component: Hello },
//route for the about route of the webpage
{ path: '/about', component: About }
]

然后在router-view之前设置router-link使点击页面不会重新加载。

spa可通过设置更多的路由和传递路径参数获得更加复杂页面。

参考https://scotch.io/tutorials/how-to-build-a-simple-single-page-application-using-vue-2-part-1

vue2搭建简易spa的更多相关文章

  1. 使用ruby搭建简易的http服务和sass环境

    使用ruby搭建简易的http服务和sass环境 由于在通常的前端开发情况下,我们会有可能需要一个http服务,当然你可以选择自己写一个node的http服务,也比较简单,比如下面的node代码: v ...

  2. Django搭建简易博客

    Django简易博客,主要实现了以下功能 连接数据库 创建超级用户与后台管理 利用django-admin-bootstrap美化界面 template,view与动态URL 多说评论功能 Markd ...

  3. EditPlus+MinGW搭建简易的C/C++开发环境

    EditPlus+MinGW搭建简易的C/C++开发环境 有时候想用C编点小程序,但是每次都要启动那难用又难看的VC实在是不情愿,而且老是会生成很多没用的中间文件,很讨厌,后来看到网上有很多人用Edi ...

  4. Python中使用Flask、MongoDB搭建简易图片服务器

    主要介绍了Python中使用Flask.MongoDB搭建简易图片服务器,本文是一个详细完整的教程,需要的朋友可以参考下 1.前期准备 通过 pip 或 easy_install 安装了 pymong ...

  5. express搭建简易web的服务器

    express搭建简易web的服务器 说到express我们就会想到nodejs,应为它是一款基于nodejs平台的web应用开发框架.既然它是基于nodejs平台的框架那么就得先安装nodejs. ...

  6. go服务端----使用dotweb框架搭建简易服务

    使用dotweb框架搭建简易服务 go语言web框架挺多的,所谓琳琅满目,里面也有很多优秀的,比如echo.beego等,但体验下来,总是觉得哪里有点小疙瘩,后来才明白过来,echo太简单,很多日常使 ...

  7. 搭建简易的c语言与python语言CGI和Apache服务器的开发环境

    搭建简易的c语言CGI和Apache服务器的开发环境 http://www.cnblogs.com/tt-0411/archive/2011/11/21/2257203.html python配置ap ...

  8. python搭建简易服务器实例参考

    有关python搭建简易服务器的方法. 需求分析: 省油宝用户数 已经破了6000,原有的静态报表 已经变得臃肿不堪, 每次打开都要缓上半天,甚至浏览器直接挂掉 采用python搭建一个最最简易的 w ...

  9. 搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3

    Web GIS系列: 搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3 使用GeoServer+QGIS发布WMTS服务 使用GeoSe ...

随机推荐

  1. 使用PinYin4j,获取汉字的拼音字母

    需要导入的文件 <!-- 引入pinyin4J的依赖 --> <dependency> <groupId>com.belerweb</groupId> ...

  2. 闭包 -------JavaScript

    本文摘要:http://www.liaoxuefeng.com/ 函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个对Array的求和.通常情况下,求和的 ...

  3. git与github账号建立SSH连接

    第1步:创建SSH Key.在用户主目录下,(就是在你的工作空间一层)看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步 ...

  4. struts2、hibernate和SSH的实现

    Struts2 为什么开发Struts框架? 为了符合更加灵活.高效的开发需求 实质上Struts2是以WebWork为核心的,他采用拦截机制来处理用户请求. (1)Jsp部分 <%@ page ...

  5. 【Django】使用list对单个或者多个字段求values值

    使用list对values进行求值: 单个字段的输出结果: price_info=list(Book.objects.filter(auth_id='Yu').values('book_price') ...

  6. java设计模式1--单例模式

    1:单例模式简介 单例模式是一种常用的软件设计模式,它确保某个类只有一个实例,而且自行实例化并向整个系统提供唯一的实例.总而言之就是在系统中只会存在一个对象,其中的数据是共享的 特点: 单例类只能有一 ...

  7. 【Ecshop】v2.7.3模板变量标签改进

    改进代码后虽可解决大多数函数参数的问题,但也同样产生了参数问题:ecshop模板函数参数有部分没有被引号包裹,所以正则并不能匹配到,要修改为引号包裹,那是个大工程. 为了使ecshop模板支持date ...

  8. PHP消息队列学习

    在我们平常网站设计时,会遇到“给用户群发短信”,“商城订单系统大批量订单处理”,“商城秒杀活动”等需求,这些功能,都有一个共同的特点:就是在面对高迸发的同时,必须要保证系统处理数据的有效性.那么如何处 ...

  9. 宏基笔记本升级bios(2012-12-28-bd 写的日志迁移

    首先到宏基官网下载中心 去下载你需要的新版本的bios安装包如图: 我的是宏基4750g的win7旗舰版64位,这里一定要根据自己的电脑的型号和安装的系统来选择,你可以选择最新的版本也可以选择老的版本 ...

  10. Python编写一个程序求2的次方

    #!/usr/bin/env python3 #-*- coding:utf-8 -*- #":"冒号后面为对参数注释,"->"为对整个函数注释 def ...