vue2搭建简易spa
使用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的更多相关文章
- 使用ruby搭建简易的http服务和sass环境
使用ruby搭建简易的http服务和sass环境 由于在通常的前端开发情况下,我们会有可能需要一个http服务,当然你可以选择自己写一个node的http服务,也比较简单,比如下面的node代码: v ...
- Django搭建简易博客
Django简易博客,主要实现了以下功能 连接数据库 创建超级用户与后台管理 利用django-admin-bootstrap美化界面 template,view与动态URL 多说评论功能 Markd ...
- EditPlus+MinGW搭建简易的C/C++开发环境
EditPlus+MinGW搭建简易的C/C++开发环境 有时候想用C编点小程序,但是每次都要启动那难用又难看的VC实在是不情愿,而且老是会生成很多没用的中间文件,很讨厌,后来看到网上有很多人用Edi ...
- Python中使用Flask、MongoDB搭建简易图片服务器
主要介绍了Python中使用Flask.MongoDB搭建简易图片服务器,本文是一个详细完整的教程,需要的朋友可以参考下 1.前期准备 通过 pip 或 easy_install 安装了 pymong ...
- express搭建简易web的服务器
express搭建简易web的服务器 说到express我们就会想到nodejs,应为它是一款基于nodejs平台的web应用开发框架.既然它是基于nodejs平台的框架那么就得先安装nodejs. ...
- go服务端----使用dotweb框架搭建简易服务
使用dotweb框架搭建简易服务 go语言web框架挺多的,所谓琳琅满目,里面也有很多优秀的,比如echo.beego等,但体验下来,总是觉得哪里有点小疙瘩,后来才明白过来,echo太简单,很多日常使 ...
- 搭建简易的c语言与python语言CGI和Apache服务器的开发环境
搭建简易的c语言CGI和Apache服务器的开发环境 http://www.cnblogs.com/tt-0411/archive/2011/11/21/2257203.html python配置ap ...
- python搭建简易服务器实例参考
有关python搭建简易服务器的方法. 需求分析: 省油宝用户数 已经破了6000,原有的静态报表 已经变得臃肿不堪, 每次打开都要缓上半天,甚至浏览器直接挂掉 采用python搭建一个最最简易的 w ...
- 搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3
Web GIS系列: 搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3 使用GeoServer+QGIS发布WMTS服务 使用GeoSe ...
随机推荐
- ios 导航视图控制器 跳转
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...
- 51nod——1402最大值、2479小b分糖果 (套路)
1402最大值:正向从1到n,如果没有限制,就依次递增1,如果有限制,就取那个限制和递增到这的最小值.这样保证1和每个限制点后面都是符合题意的递增,但是限制点前面这个位置可能会有落差(之前递增多了). ...
- 精致的系统监控工具-netdata
今天在网上瞎逛,偶然发现一款监控工具:netdata,感到一惊,监控工具竟然可以这么漂亮! 简单了解一下,这款工具还算比较新,监控系统运行状态的功能非常强大,除了监控cpu,网卡,磁盘,内存,进程等等 ...
- SSM(Spring+Spring MVC+Mybatis)开发前台后功能完整的java开源博客管理系统
项目描述 本项目通过SSM(SpringMVC+Mybatis+Spring)框架编写的一个人博客管理系统,使用hexo主题,以及MAVEN进行对项目管理,并且前端具有粒子和点击爱心效果.后端的页面框 ...
- python读取xls文件
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/10/17 14:41 # @Author : Sa.Song # @Desc ...
- [原]sencha touch之表单(login demo)
现在来说说sencha touch中的表单,举个简单的login的例子,相关的说明我都放在了注释中,看下面代码 Ext.application({ id:'itKingApp', launch:fun ...
- v-model 的修饰符
1..trim 自动过滤输入内容最开始 和 最后的 空格,中间的会保留一个空格,多的会被过滤掉 2..lazy 一般情况下,在input的 v-model是一直在同步 输入的内容与显示的内容,不过再添 ...
- HDU 1535 S-Nim(SG函数)
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 基于web自动化测试框架的设计与开发(本科论文word)
- Oracle 学习----:创建表(主键自增)
一.创建表 create table testTable ( Id numbere, name varchar2(100), age number, createTime date, primary ...