接下来我们要做的是vue的路由处理,首先当然是安装:

npm install vue-router

接下打开我们的main.js,引入我们vue-router,然后在告诉vue我们要使用我们的vue-router

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App' Vue.use(VueRouter) /* eslint-disable no-new */
new Vue({
el: '#app',
template: '<App/>',
components: { App }
})

接下来我们开始使用,比如我们的首页我们是用我们的todo-items这个组件来处理

const routes=[
{path:'/',component:todoItems},
]

那么我们当然也要把它引用进来

import todoItem from './components/Todo-items'

接下来我们还要做一件事情就是访问一个todo跟上一个id

const routes=[
{path:'/',component:todoItem},
{path:'/todo/:id',component:Todo}
]

但是我们并没有todo这个组件所以创建一个toto的组件

<template>

</template>

<script>
export default {
data () {
return {
}
},
}
</script>

在main.js中也是要引用进来的

import Todo from './components/todo'

然后实例化我们的router

const router = new VueRouter({
routes
})

但是我们还要告诉的们vue我们要把router传递进去的

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App'
import todoItem from './components/Todo-items'
import Todo from './components/todo' Vue.use(VueRouter) const routes=[
{path:'/',component:todoItem},
{path:'/todo/:id',component:Todo,name:'todo'}
] const router = new VueRouter({
routes
})
/* eslint-disable no-new */
new Vue({
el: '#app',
template: '<App/>',
components: { App },
router
})

然后我们的视图内容交给我们router来处理,也就是我们在app.vue中的组件引用那块,不直接渲染

<template>
<div id="app">
<img src="./assets/logo.png">
<!-- <hello></hello> -->
<!-- <todo-item :todos="todos"></todo-item>
<todo-form :todos="todos"></todo-form> -->
<router-view :todos="todos"></router-view>
</div>
</template> <script>
import Hello from './components/Hello'
// import todoForm from './components/todo-form'
// import todoItem from './components/Todo-items' export default {
name: 'app',
data(){
return{
message:'this is todos',
todos:[
{id:1,title:"learn vuejs",completed:false},
],
}
},
computed:{
todoCount(){
return this.todos.length;
}
},
components: {
Hello
}
}
</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>

但是这个时候我们的form并没有显示,那么我们就在首页的todoItem中引用这个子组件,script里面的代码

<script>

import todoForm from './todo-form'

export default {
name:'todos',
props:['todos'],
data(){
return {
newTodo:{id:null,title:"",completed:false}
}//定义一个obj;
},
props:['todos'],
methods:{
deleteTodo(index){
this.todos.splice(index,1);//删除下标为index的元素
},
toggleCompletion(todo){
todo.completed = !todo.completed;
}
},
components: {
todoForm
}
}
</script>

当然也要在template中引用这个组件

<template>
<div id="todos">
<ul class="list-group">
<li class="list-group-item" v-for="(todo,index) in todos" v-bind:class="{'completed' : todo.completed}">
<router-link :to="{ name: 'todo', params: { id:todo.id }}">{{todo.title}} </router-link>
<button class="btn btn-warning btn-xs pull-right" v-on:click="deleteTodo(index)">Delete</button>
<button class="btn btn-xs pull-right margin-right-10" v-on:click="toggleCompletion(todo)" v-bind:class="[todo.completed ? 'btn-success' : 'btn-danger']">{{todo.completed ? 'completed' : 'working'}}</button>
</li>
</ul> <todo-form :todos="todos"></todo-form>
</div>
</template> <script> import todoForm from './todo-form' export default {
name:'todos',
props:['todos'],
data(){
return {
newTodo:{id:null,title:"",completed:false}
}//定义一个obj;
},
props:['todos'],
methods:{
deleteTodo(index){
this.todos.splice(index,1);//删除下标为index的元素
},
toggleCompletion(todo){
todo.completed = !todo.completed;
}
},
components: {
todoForm
}
}
</script> <style>
.completed{
color: green;
font-style: italic;
}
.margin-right-10{
margin-right: 10px;
}
</style>

vuejs,router的更多相关文章

  1. weex官方demo weex-hackernews代码解读(下)

    weex 是阿里出品的一个类似RN的框架,可以使用前端技术来开发移动应用,实现一份代码支持H5,IOS和Android.而weex-hacknews则是weex官方出品的,首个使用 Weex 和 Vu ...

  2. 【vuejs小项目——vuejs2.0版本】单页面搭建

    http://router.vuejs.org/zh-cn/essentials/nested-routes.html 使用嵌套路由开发,这里会出错主要把Vue.use(VueRouter);要进行引 ...

  3. Vuejs使用笔记 --- 框架

    这两天学了vuejs的模板,于此纪录一下. 这是整个大文件夹的配置,现在我们看到的所有文件都不需要去管,说要关注也只需要关注“index.html” "index.html"里面是 ...

  4. VueJs一些资料网站链接

    https://github.com/liukaijv/laravel-vue-cmshttps://github.com/allan2coder/VUE2-SPA-Tutorialhttps://g ...

  5. VueJs开发笔记—IDE选择和WebStorm性能优化、框架特性和数据调用、路由选项以及使用

    一.IDE的选择: VsCode和WebStorm都是不错的选择,两者运行调试都非常的方便都可以使用快捷键运行和停止,就打开项目的速度和对电脑配置的要求来说,vscode要比webstorm要出色很多 ...

  6. Vuejs实例-01使用vue-cli脚手架搭建Vue.js项目

    [TOC] 1. 前言 vue-cli 一个简单的构建Vue.js项目的命令行界面 整体过程: $ npm install -g vue-cli $ vue init webpack vue-admi ...

  7. vue初级学习--路由router的编写(resolve的使用)

    一.导语 最近在用vue仿写淘宝的商品详情页面以及加入购物车页面,若是成功了,分享给大家~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 二.正文 我先用控制台创建了vue项目demo(如 ...

  8. 三、VueJs 填坑日记之项目文件认识

    上一篇博文,我们搭建了一套基础的vuejs的环境,首先安装node.js,然后利用npm包管理器,安装vue-cli,设置淘宝镜像,初始化项目,安装依赖,运行.在这一篇,我们将认识vuejs项目里的各 ...

  9. 七、VueJs 填坑日记之渲染一个列表

    在上一篇博文中,我们对vue组件有了一个简单的认识和大概的理解.在之前认识项目结构的时候,我们在/src目录中创建了一个components的文件夹,而今天就要用到了,这个文件夹的作用就是放置我们的自 ...

随机推荐

  1. HTML5新增标签及具体用法

    html5自从推广普及以来,迅速被各大浏览器支持.采用html5设计的网页逐渐成为网页设计的时尚.下面就温习下html5的新增标签. HTML 5 中的新特性包括了嵌入音频.视频和图形的功能,客户端数 ...

  2. test latex1

    equation systems: \begin{equation} 1 + 2 = 3 \ 1 = 3 - 2 \end{equation} align text \begin{align} 1+2 ...

  3. /date(-62135596800000)转换正常格式的时间

    function formatDatebox(value) { if (value == null || value == '') { return ''; } var dt = parseToDat ...

  4. 如何清除SQL Server Management Studio的最近服务器列表

    SQL Server Management Studio (SSMS) 的"连接到服务器"对话框会记录用户所有访问过的服务器名称,这个功能对于经常连接多个数据库的人来说确实挺方便的 ...

  5. Add and Search Word

    Trie 树的一个应用 Design a data structure that supports the following two operations: void addWord(word) b ...

  6. NGUI实现ScrollView功能

    NGUI,目前Unity3D Assert Store中最火的2D图形界面工具. 本文假设读者有Unity3D使用经验.有基本了解.NGUI3.6.0 1.新建Pannel(Scroll View), ...

  7. xmind的第十一天笔记

  8. 拓展Yii Framework(易框架)

    1.拓展yii 此文针对Yii1.1.15而写,请注意甄别你的Yii Framework 版本. 拓展yii是开发期间常见的代码处理方式.例如,你写一个新的controller(业务控制器),你通过继 ...

  9. js将金额专成每隔3位数加逗号

    js将金额专成每隔3位数加逗号,比如 12345.00 转成 12,345.00; 懒得解释具体代码如下 //分割 String.prototype.joinByNum = function(num, ...

  10. JAVA的单例模式与延时加载

    延迟加载(lazy load)是(也称为懒加载),也叫延迟实例化,延迟初始化等,主要表达的思想就是:把对象的创建延迟到使用的时候创建,而不是对象实例化的时候创建.延迟加载机制是为了避免一些无谓的性能开 ...