2015-10-12 98 views
0

所以我基本上有2个脚本。处理用户登录,检查等的用户脚本。还有一个处理页面操作的脚本。现在,我希望能够显示由页面脚本控制的按钮,但只有在用户登录时才显示该按钮。此按钮位于页面脚本元素中,因此我无法通过用户脚本对其进行处理。这也会非常混乱。从其他Vue.js脚本获取数据

继承人一些代码来解释什么,我试图做:

user.js的:

var userAuth = new Vue({ 

    el: '#userSegment', 
    data: { 'loggedin': false, }, 
    ready: function(){ 
    if(userLoggedIn){ this.loggedin = true; }else{ this.loggedin = false; } 
    }, 

}); 

page.js

new Vue({ 

el: '#body', 
data: { 'buttonclicked': false, }, 
method: {  
clicked: function(){ this.buttonclicked = true; }, 
}, 

}); 

的index.html:

<html> 
<div id='userSegment></div> 

<div id='body'> 
    <button v-if='userAuth.loggedIn' v-on='click: clicked()' > 
    Click Me 
    </button> 
</div> 

//both the script links are here. Dont worrie 

</html> 

但按钮没有显示当用户登录时。对不起,如果解决方案非常简单,但这个框架的文档(比如每4个5)都很糟糕并且一团糟。

回答

1

有几种不同的方式来完成这种类型的行动,但主要的概念是,你将要到主数据集,所有相关的功能将依赖于当一些与用户更改被修改。在这种情况下,你的数据集是用户信息:

// This would be the master global user JS object 
var userAuthGlobals = { 
    loggedIn: false 
}; 

var userAuth = new Vue({ 
    el: '#userSegment', 
    ready: function(){ 
     // Setting the global user auth 
     if(userLoggedIn) { 
      userAuthGlobals = true; 
     } else { 
      userAuthGlobals = false; 
     } 
    } 
}); 

var body = new Vue({ 
    el: '#body', 
    // Relies on the global user auth 
    data: userAuthGlobals, 
    methods: { 
     clicked: function(){ this.buttonclicked = true; } 
    } 
});  

<html> 
    <div id='userSegment></div> 

    <div id='body'> 
     <button v-if='loggedIn' v-on='click: clicked' > 
      Click Me 
     </button> 
    </div> 
</html>