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

时间: 2024-08-07 02:21:01

vue2搭建简易spa的相关文章

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

有关python搭建简易服务器的方法. 需求分析: 省油宝用户数 已经破了6000,原有的静态报表 已经变得臃肿不堪, 每次打开都要缓上半天,甚至浏览器直接挂掉 采用python搭建一个最最简易的 web 服务 请求一个nick 就返回 对应的 报表数据 参数用GET方式传送 调研与实现: 园里没找到靠谱的,google了半天,最终还是成功了. 以下是源码,里面记录了 其中的 一些问题 复制代码 代码如下: #! /usr/bin/env python # -*- coding: utf-8 -

express搭建简易web的服务器

express搭建简易web的服务器 说到express我们就会想到nodejs,应为它是一款基于nodejs平台的web应用开发框架.既然它是基于nodejs平台的框架那么就得先安装nodejs. 先到(nodejs.org)这个网站下载,点击如下图处下载任意一版,下载好以后解压安装. 创建一个文件夹,在其文件中(shift+右键)打开命令行工具通过(npm init) 命令为你的应用创建一个package.json 文件.在创建 package.json 文件时有以下步骤. (shift+右

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

使用ruby搭建简易的http服务和sass环境 由于在通常的前端开发情况下,我们会有可能需要一个http服务,当然你可以选择自己写一个node的http服务,也比较简单,比如下面的node代码: var PORT = 3000; var http = require('http'); var url=require('url'); var fs=require('fs'); var mine= { "css": "text/css", "gif"

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

搭建简易Web GIS网站:使用GeoServer+PostgreSQL+PostGIS+OpenLayers3 1         服务器搭建 使用Tomcat需要先安装Java.从Oracle官方网站下载Java最新版本: http://www.oracle.com/technetwork/java/javase/downloads/index.html 安装完成Java后,需要配置环境变量.具体方法请自行百度.安装成功后可以下载服务器了. 从Tomcat官方网站下载最新的版本,在Windo

Django初体验——搭建简易blog

前几天在网上看到了篇采用Django搭建简易博客的视频,好奇心驱使也就点进去学了下,毕竟自己对于Django是无比敬畏的,并不是很了解,来次初体验. 本文的操作环境:ubuntu.python2.7.Django1.8.6.Pycharm5.其实自从使用了ubuntu之后就很神奇的喜欢上了它,真的用起来方便很多. 1.Django项目文件并创建blog应用 (1)可以在终端中建立Django项目,使用django-admin startproject ...直接上图: 建立blog应用: (2)

ubuntu 搭建简易的https网站

ubuntu 搭建简易的https网站 环境:ubuntu 12.04.5 openssl (1)创建一个ssl的保存路径 sudo mkdir /opt/nginx/ssl (2)生存密钥sudo openssl genrsa -out key.pem 2048 (3)sudo openssl req -new -x509 -nodes -out server.crt -keyout server.key (4)配置nginx server { listen 443; index index.

Servlet+oracle MVC 架构 搭建简易购物车web项目---数据库设计

Servlet+oracle MVC 架构 搭建简易购物车web项目 主要实现以下功能: 1.用户登录,从数据库验证用户的合法性. 2.购物大厅,从数据库取出商品进行展示. 3.在购物大厅可以点击购买商品,跳到我的购物车界面. 4.在我的购物车页面,可以更新商品数量,并能够计算商品总价.可以删除商品.可以提交订单. 5.提交订单以后,进入订单页面,展示个人信息和订单信息  6.再次提交订单以后,给用户发送电子邮件,提醒用户. 数据库设计 用户表 create table users ( id n

搭建简易堡垒机

笔记内容:搭建简易堡垒机笔记日期:2018-01-18 23.1 什么是堡垒机 23.2 搭建简易堡垒机 23.3 安装jailkit实现chroot 23.4 日志审计 23.1 什么是堡垒机 堡垒机,是在一个特定的网络环境下,为了保障网络和数据不受来自外部和内部用户的入侵和破坏,而运用各种技术手段实时收集和监控网络环境中每一个组成部分的系统状态.安全事件.网络活动,以便集中报警.及时处理及审计定责. 我们又把堡垒机叫做跳板机,简易的跳板机功能简单,主要核心功能是远程登录服务器和日志审计.运维

理解tomcat之搭建简易http服务器

做过java web的同学都对tomcat非常熟悉.我们在使用tomcat带来的便利的同时,是否想过tomcat是如何工作的呢?tomcat本质是一个http服务器,本篇文章将搭建一个简单的http服务器. 1 Catalina模型 首先我们先了解一下tomcat的大致工作原理.tomcat的核心是servlet容器,我们称它为Catalina(为什么叫这个名字?我也不知道 ̄へ ̄).模型图如1.1 图1.1 Connector是用来"连接"容器里边的请求的.它的工作是为接收到每一个 H