初学者对Vue路由与组件运用

2 篇文章 0 订阅
2 篇文章 0 订阅

Vue路由与组件运用

代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>商品首页</title>
		<link rel="stylesheet" href="css/shopXq.css">
		<script src="js/vue.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="node_modules/vue-router/dist/vue-router.min.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div id="container">
		<!--通过router-view指定盛放组件的容器 -->
			<router-view></router-view>
		</div>
		 <script>
			  var testLogin = Vue.component("login",{
			   template:`
			    <div>
			     <h1>商品列表</h1>
				 <table>
				         <thead>
				             <tr>
				                 <th></th>
				                 <th>书籍名称</th>
				                 <th>价格</th>
				                 <th>购买数量</th>
				                 <th>操作</th>
				             </tr>
				             
				         </thead>
				         <tbody>
				             <tr v-for = "(item,index) in book">
				                 <td>{{item.id}}</td>
				                 <td>{{item.name}}</td>
				                 <td>{{item.price}}</td>
				                 <td>
				                     <button @click = "btn(index)" v-bind:disabled = "item.count <= 1">-</button>
				                          {{item.count}}
				                     <button @click = "btns(index)">+</button>
				                 </td>
				                 <td>
				                     <router-link :to="{path:'myRegister',query: { id: item.id,name:item.name,price:item.price,count:item.count }}">加入购物车</router-link>
				                 </td>
				             </tr>
				         </tbody>
						 <h2>总价格 {{totalprice}}</h2>
				 </table>
			    </div>
			   `,
			   data:function(){
				   return {
					   book:[
					       {
					           id :1,
					           name:'《Java从入门到精通》',
					           price:85+".00",
					           count:1
					       },
					       {
					           id :2,
					           name:'《C语言从入门到精通》',
					           price:59+".00",
					           count:1
					       },
					       {
					           id :3,
					           name:'《C++从入门到精通》',
					           price:39+".00",
					           count:1
					       },
					       {
					           id :4,
					           name:'《PHP从入门到精通》',
					           price:128+".00",
					           count:1
					       }
					   ],
				   }
			   },
			   methods:{
				   btn(index){
				       this.book[index].count--;
				   },
				   btns(index){
				       this.book[index].count++
				   },
			   },
			   computed:{
				   totalprice:function(){
					   let result = 0
					   for(let i =0;i<this.book.length;i++){
					   	result += this.book[i].price * this.book[i].count
					   }
					   return result+".00"
				   }
			   }
			   /*to后面是路由地址*/
			  })
			  var testRegister = Vue.component("register",{
			   methods:{
			    jumpToLogin:function(){
			     this.$router.push('/myLogin');
			    }
			   },
			   template:`
			    <div>
			     <h1>商品详情</h1>
				 <table>
				         <thead>
				             <tr>
				                 <th></th>
				                 <th>书籍名称</th>
				                 <th>价格</th>
				                 <th>购买数量</th>
				             </tr>
				             
				         </thead>
				         <tbody>
				             <tr>
				                 <td>{{this.$route.query.id}}</td>
				                 <td>{{this.$route.query.name}}</td>
				                 <td>{{this.$route.query.price}}</td>
				                 <td>
				                      {{this.$route.query.count}}
				                 </td>
				             </tr>
				         </tbody>
				 		<h2>总价格 {{totalprice}}</h2>
				 </table>
			     <button @click="jumpToLogin">返回</button>
			    </div>
			   `,
			   computed:{
			   			   totalprice:function(){
							   var result = 0;
			   				   result += this.$route.query.price * this.$route.query.count;
			   				   return result+".00"
			   			   }
			   }
			  })
			  //配置路由词典
			  const myRoutes =[
			   {path:'',component:testLogin},
			   {path:'/myLogin',component:testLogin},
			   {path:'/myRegister',component:testRegister}
			  ]
			 
			  const myRouter = new VueRouter({
			   routes:myRoutes
			  })
			  new Vue({
			   router:myRouter,
			   el:"#container",
			   data:{
			    
			   }
			  })
			 </script>
		</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值