Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

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

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

var joinRoom = $('#joinRoom'),joinRoomLabel=$('#joinRoomLabel'),curRoomId,
onlineAlert=$('#onlineAlert'),offlineAlert=$('#offlineAlert'),chatLeft=$('#chatLeft');
Vue.component('room',{
  props:['message'],
  methods:{
    selectRoom:function(){

            chatLeft.empty();
            curRoomId = this.message._id;
            joinRoomLabel.text(this.message.roomName);

            joinRoom.modal('toggle');
            socket.emit('online', {user: from, curRoomId: curRoomId});
            vRoom.getUsers();


    }
  },
  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({
  el:'#joinRoom',
  data:{
    users:[],
    messageBody:'',
    roomChat:'',
    roomJoinPeos:''
  },
  methods:{
    upUsers:function(data){
      this.users = [];
      var us = [];
      for(var u in data){
        us.push(u);
      }
      if(/*message.roomChat*/=='secret'&&us.length==3){
            alert("房间已满,但在同一房间的人会知道您曾经尝试进入该房间");
            joinRoom.modal('toggle');
            this.closeRoom();
            return;
        }
      this.users = us;
    },
    getUsers:function(){
      $.get('/room/users/'+curRoomId,function(data){
        this.upUsers(data);
      }.bind(this));
    },
    sendMessage:function(){
        if(this.messageBody){
          chatLeft.append(' <div class="chatLeftOwenr"><div class="chatLeftOwenrPeo">'+from+'</div><div class="chatLeftOwenrContent">'+this.messageBody+'</div></div>');
          socket.emit('say',{toObj:'all',messageBody:this.messageBody,curRoomId:curRoomId,user:from});
          this.messageBody='';
        }
    },
    closeRoom:function(){
        socket.emit('offline',{curRoomId:curRoomId,user:from});
        curRoomId = '';
    }
  }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

我用了一个比较tricky的方式解决,但并不是真正的解决方案。

var joinRoom = $('#joinRoom'),joinRoomLabel=$('#joinRoomLabel'),curRoomId,
onlineAlert=$('#onlineAlert'),offlineAlert=$('#offlineAlert'),chatLeft=$('#chatLeft');
Vue.component('room',{
  props:['message'],
  methods:{
    selectRoom:function(){

            chatLeft.empty();
            curRoomId = this.message._id;
            joinRoomLabel.text(this.message.roomName);

            joinRoom.modal('toggle');
            socket.emit('online', {user: from, curRoomId: curRoomId});
            if(this.message.roomChat=='secret'){
                vRoom.getUsers();
            }else{
                vRoom.getpUsers();
            }
    }
  },
  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({
  el:'#joinRoom',
  data:{
    users:[],
    messageBody:'',
    roomChat:'',
    roomJoinPeos:''
  },
  methods:{
    upUsers:function(data){
      this.users = [];
      var us = [];
      for(var u in data){
        us.push(u);
      }
      if(us.length==3){
            alert("房间已满,但在同一房间的人会知道您曾经尝试进入该房间");
            joinRoom.modal('toggle');
            this.closeRoom();
            return;
        }
      this.users = us;
    },
    getUsers:function(){
      $.get('/room/users/'+curRoomId,function(data){
        this.upUsers(data);
      }.bind(this));
    },
      uppUsers:function(data){
          this.users = [];
          var us = [];
          for(var u in data){
              us.push(u);
          }
          this.users = us;
      },
      getpUsers:function(){
          $.get('/room/users/'+curRoomId,function(data){
              this.uppUsers(data);
          }.bind(this));
      },
    sendMessage:function(){
        if(this.messageBody){
          chatLeft.append(' <div class="chatLeftOwenr"><div class="chatLeftOwenrPeo">'+from+'</div><div class="chatLeftOwenrContent">'+this.messageBody+'</div></div>');
          socket.emit('say',{toObj:'all',messageBody:this.messageBody,curRoomId:curRoomId,user:from});
          this.messageBody='';
        }
    },
    closeRoom:function(){
        socket.emit('offline',{curRoomId:curRoomId,user:from});
        curRoomId = '';
    }
  }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...