猿问

有没有办法在new vue里引用vue的组件中的值?

原谅我没有办法准确的询问问题,我暂时只是个vue初学者,而且我其实也看不太懂教程(正在努力尝试理解当中。。)
我在研究改进nodejs的一个聊天室demo,有公聊私聊功能(多房间的),关于私聊我想在公聊房间的基础上加个房间内人数的判断即了事。现在情况是这样的,我想在vRoommethods当中引用一个值,这个值在component当中是this.message.roomChat(我用这个值来判断公聊私聊),在vRoommethodsupUsers方法当中的message.roomChat(这种写法是错的)就是component的那个值。求各位大神解答?

富国沪深
浏览 588回答 1
1回答

蛊毒传说

我用了一个比较tricky的方式解决,但并不是真正的解决方案。var joinRoom = $('#joinRoom'),joinRoomLabel=$('#joinRoomLabel'),curRoomId,onlineAlert=$('#onlineAlert'),offlineAlert=$('#offlineAlert'),chatLeft=$('#chatLeft');Vue.component('room',{&nbsp; props:['message'],&nbsp; methods:{&nbsp; &nbsp; selectRoom:function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chatLeft.empty();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curRoomId = this.message._id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; joinRoomLabel.text(this.message.roomName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; joinRoom.modal('toggle');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.emit('online', {user: from, curRoomId: curRoomId});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(this.message.roomChat=='secret'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vRoom.getUsers();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vRoom.getpUsers();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; },&nbsp; template: '<div class="room"><div class="room-header"><div class="room-header-back"></div><div class="room-header-content"><div>{{message.roomName}}</div><div><span class="glyphicon glyphicon-plus-sign" v-on:click="selectRoom"></span></div></div></div><div class="room-body"><img src="/public/imgs/0.png"><div class="room-text">{{message.roomContent}}</div></div><div class="room-footer"><div class="room-footer-back"></div><div class="room-footer-content"><div class="room-peos">参与:<span>{{message.roomJoinPeos}}人</span></div><div><span class="glyphicon glyphicon-heart"></span></div> </div></div></div>'});var vRoom = new Vue({&nbsp; el:'#joinRoom',&nbsp; data:{&nbsp; &nbsp; users:[],&nbsp; &nbsp; messageBody:'',&nbsp; &nbsp; roomChat:'',&nbsp; &nbsp; roomJoinPeos:''&nbsp; },&nbsp; methods:{&nbsp; &nbsp; upUsers:function(data){&nbsp; &nbsp; &nbsp; this.users = [];&nbsp; &nbsp; &nbsp; var us = [];&nbsp; &nbsp; &nbsp; for(var u in data){&nbsp; &nbsp; &nbsp; &nbsp; us.push(u);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(us.length==3){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("房间已满,但在同一房间的人会知道您曾经尝试进入该房间");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; joinRoom.modal('toggle');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.closeRoom();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; this.users = us;&nbsp; &nbsp; },&nbsp; &nbsp; getUsers:function(){&nbsp; &nbsp; &nbsp; $.get('/room/users/'+curRoomId,function(data){&nbsp; &nbsp; &nbsp; &nbsp; this.upUsers(data);&nbsp; &nbsp; &nbsp; }.bind(this));&nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; uppUsers:function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.users = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var us = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var u in data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; us.push(u);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.users = us;&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; getpUsers:function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.get('/room/users/'+curRoomId,function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.uppUsers(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }.bind(this));&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; sendMessage:function(){&nbsp; &nbsp; &nbsp; &nbsp; if(this.messageBody){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chatLeft.append(' <div class="chatLeftOwenr"><div class="chatLeftOwenrPeo">'+from+'</div><div class="chatLeftOwenrContent">'+this.messageBody+'</div></div>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.emit('say',{toObj:'all',messageBody:this.messageBody,curRoomId:curRoomId,user:from});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.messageBody='';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; closeRoom:function(){&nbsp; &nbsp; &nbsp; &nbsp; socket.emit('offline',{curRoomId:curRoomId,user:from});&nbsp; &nbsp; &nbsp; &nbsp; curRoomId = '';&nbsp; &nbsp; }&nbsp; }})
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答