接下来我们要做的是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. "****" is not translated in zh, zh_CN.的解决方法

    最近在开发一个app,要用到静默安装等一些小技术,但是引发了问题如下: 在Android SDK Tool r19之后, Export的时候遇到xxx is not translated in yyy ...

  2. css3-实现3D立方体旋转

    核心内容: 1.CSS3 中 animation.perspective 属性的熟练运用. 2.CSS3 中的变形属性 transform,在 3D 立体效果中的运用. 3.3D 立方体旋转实现原理. ...

  3. 《深入浅出Node.js》第6章 理解 Buffer

    @by Ruth92(转载请注明出处) 第6章 理解 Buffer ✁ 为什么需要 Buffer? 在 Node 中,应用需要处理网络协议.操作数据库.处理图片.接收上传文件等,在网络流和文件的操作中 ...

  4. Sublime Text2 快捷键汇总

    一个好的编辑器,能大大提高编程的效率.如果能熟知软件的快捷键,那更能让你得心印手.这些内容都是我网上和自己实际使用过程中所收集而来的,在网络上应该也算比较全面的了吧.欢迎大家补充,我也会在以后慢慢添加 ...

  5. python基础(1)

    一.应用 python应用:数据分析.组件集成.网络服务.图像处理.数值计算和科学计算. 使用python的企业:YouTube.dropbox.BT.Quora.豆瓣.知乎.google.Yahoo ...

  6. 什么是Ajax

    AJAX不是一种新的编程语言,而是一种用于创建更好更快以及交互性更强的Web应用程序的技术. 使用Javascript向服务器提出请求并处理响应而不阻塞用户!核心对象XMLHTTPRequest.通过 ...

  7. 补交作业-第八周PSP

    一.表格 C(分类) C(内容) S(开始时间) ST(结束时间) I(打断时间) △(净工作时间) 讨论 用户界面 9:30 10:40 15 55 编码 编码 13:20 16:30 10 180 ...

  8. BZOJ4724 [POI2017]Podzielno

    4724: [POI2017]Podzielno Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 77  Solved: 37[Submit][Stat ...

  9. 写好unit test的建议和例子

    最近翻了下写unit test 的文章,总结如下 What's unit test? "Unit testing is a software testing method by which ...

  10. mysql relay log参数汇总

    前言:MySQL进行主主复制或主从复制的时候会在配置文件制定的目录下面产生相应的relay log,本文档总结这些相关参数的定义及解释. 1.什么是relay log The relay log, l ...