zoukankan      html  css  js  c++  java
  • 如何用vue的computed的set和get方法

    如何用vue的computed的set和get方法
    默认只有getter可以获取值
    对计算属性设置值,会调用计算属性的setter方法

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<title></title>
    		<script src="../node_modules/vue/dist/vue.js"></script>
    		<style>
    			li{list-style: none;}
    			.active{background:red;}
    		</style>
    	</head>
    	<body>
    		<div id="app">
    			<audio :src="getCurrentsong" autoplay="autoplay" controls="controls"></audio>
    			<ul>
    				<li v-for="(song,index) in musicData" :class="{active:index==currentIndex}"  @click="clickHandler(index)">
    					<h2>{{song.id}}--{{song.name}}</h2>
    					<h3>{{song.songSrc}}</h3>
    				</li>
    			</ul>
    		</div>
    		<script type="text/javascript">
    			var musicData = [
    			    {
    			        id: 1,
    			        name: "I believe",
    			        songSrc: "../music/I believe.mp3",
    			        author: "杨培安"
    			    },
    			    {
    			        id: 2,
    			        name: "一百万个可能.mp3",
    			        songSrc: "../music/一百万个可能.mp3",
    			        author: "Skot Suyama"
    			    },
    			    {
    			        id: 3,
    			        name: "I believe",
    			        songSrc: "../music/I believe.mp3",
    			        author: "杨培安"
    			    }
    			];
    			new Vue({
    				el:"#app",
    				data(){
    					return{
    						musicData,
    						currentIndex:0
    					}
    				},
    				methods:{
    					clickHandler(index){
    						//可以获取computed的方法的值
    						this.getCurrentsong=index;
    					}
    				},
    				computed:{
    					getCurrentsong:{
    						get:function(){
    							return this.musicData[this.currentIndex].songSrc
    						},
    						set:function(val){
    							this.currentIndex=val;
    						}		
    					}
    				}
    			})
    		</script>
    	</body>
    </html>
    
    
  • 相关阅读:
    设计模式之访问者模式
    设计模式之命令模式
    设计模式之迭代器模式
    tomcat8.0.11性能优化
    java 基础 --集合--012
    StringBuffer和StringBuilder的区别
    jquery 入门
    java 基础 --匿名内部类-008
    java 基础 --多态--009
    java 基础--继承--007
  • 原文地址:https://www.cnblogs.com/lxystar/p/10724481.html
Copyright © 2011-2022 走看看