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 ...
随机推荐
- HTML 5新元素和CSS
Html5 新元素 多媒体元素 video/audio: 格式例子: 属性: canvas元素 Canvas标签定义图形,用于图形的绘制,使用 js来绘图 拖放drag和drop 拖放是一种常见 ...
- java面向对象思想2
1.主函数是一类特殊的函数,作为程序入口,可被虚拟机调用.主函数格式是固定的.public:函数访问权限最大.static:代表函数随着类的加载已经存在.void:主函数没有具体返回值.main:不是 ...
- 第17题:打印1到最大的n位数
面试题17:打印1到最大的n位数 题目:输入数字n,按顺序打印出从1最大的n位十进制数.比如输入3,则打印出1.2.3一直到最大的3位数即999. 考点: 用字符串或者数组表达一个大数. 思路 1. ...
- redhat linux6.5升级openssh
1.下载最新的openssh包 http://www.openssh.com/portable.html#http 2.升级openssh之前要先打开服务器telnet,通过telnet登录服务器,因 ...
- SpringMVC 多视图解析器 跳转问题
在SpringMVC的配置文件中加入以下配置: <!-- 下面红色的配置必须要在--> <mvc:default-servlet-handler /> <bean id ...
- 浅谈JavaScript字符串拼接
本文给大家汇总介绍了几种javascript中字符串拼接的方法,十分的简单实用,有需要的小伙伴可以参考下. 在JavaScript中会经常遇到字符串拼接,但是如果要拼接的字符串过长就比较麻烦了. 如果 ...
- PS1
linux系统终端命令提示符设置(PS1)记录 - 散尽浮华 - 博客园 https://www.cnblogs.com/kevingrace/p/5985970.html PS(Prompt Sig ...
- jupyter notebook(二)——修改jupyter打开默认的工作目录
1.简述 jupyter notebook,启动后,浏览器发现工作目录并不是自己真正的代码的工作路径.所以需要设置一下.这样方便自己快捷使用. 2.设置修改jupyter notebook打开后默认工 ...
- javascript sprintf方法
转载自: http://demon.tw/programming/javascript-sprintf.html function str_repeat(i, m) { for (var o = [] ...
- JZOJ 5196. 【NOIP2017提高组模拟7.3】B
5196. [NOIP2017提高组模拟7.3]B Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto Pro ...