computed属性默认只有 getter ,不过在需要时你也可以提供一个 setter 。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>computed setter</title> <script src="vue.js"></script> </head> <body> <div id="app"> <p>{{site}}</p> </div> <script> let vm = new Vue({ el:'#app', data:{ name:'Baidu', url:'http://www.baidu.com' }, computed:{ site:{ get(){ return this.name + '——' + this.url }, set(newValue){ let names = newValue.split(' ') this.name = names[0] this.url = names[names.length - 1] } } } }) // 调用 setter, vm.name 和 vm.url 也会被对应更新 vm.site = '菜鸟教程 http://www.runoob.com'; document.write('name: ' + vm.name); document.write('<br>'); document.write('url: ' + vm.url); </script> </body> </html>