简体   繁体   English

如果用户登录或未登录,则显示不同的列表项

[英]Show different list items if user is logged in or not

I have an array of possible side nav items that i want to render to the screen.我有一系列可能的侧面导航项目,我想渲染到屏幕上。 The guest user should only see 2 of the items and when the user logs in he should be able to see all 3 of them.来宾用户应该只能看到 2 个项目,当用户登录时,他应该能够看到所有 3 个项目。 How would I not render the first item in the list if the user is not logged in?如果用户未登录,我将如何不呈现列表中的第一项?

apps: Array<SideNavItemModel> = [
{
  title: "sideNav.centralWeighingLink",
  url: "centralWeighing",
  id: "1",
  icon: "mdi-scale-balance",
  isAuthorized: false
},
{
  title: "sideNav.appsLink",
  url: "apps",
  id: "2",
  icon: "mdi-apps"
},
{
  title: "sideNav.2Link",
  url: "password",
  id: "3",
  icon: "mdi-key-variant"
},
]; 

I have a mounted method that checks if the $auth.user object is not null, and it then sets the value of isAuthorized to true.我有一个挂载的方法来检查 $auth.user object 是否不是 null,然后将 isAuthorized 的值设置为 true。

This is the code that renders the list now:这是现在呈现列表的代码:

    <v-list dense>
    <v-list-item v-for="app in apps" :key="app.id"
                 active-class="router-link-active" :to="`/${app.url}`" link>
      <div v-if="!$auth.user && app.isAuthorized === false ">
        <v-list-item-icon>
          <v-icon>{{ app.icon }}</v-icon>
        </v-list-item-icon>
        <v-list-item-content>
          <v-list-item-title>{{ $t(app.title) }}</v-list-item-title>
        </v-list-item-content>
      </div>
    </v-list-item>
  </v-list>

I dont know how to write this block so that it would ommit the first item in the list without hardcodding it somehove, since this line below doesnt work.我不知道如何编写此块,以便它会省略列表中的第一项而不会将其硬编码,因为下面的这一行不起作用。

v-if=".$auth.user && app.isAuthorized === false v-if=".$auth.user && app.isAuthorized === false

I know that using the loggedIn method would be easier but I cannot used it because of the way the app is built currently.我知道使用 loggedIn 方法会更容易,但由于当前构建应用程序的方式,我无法使用它。

If you want to show the first item after user logged in, just need to change the condition of the first item.如果要在用户登录后显示第一项,只需更改第一项的条件即可。

Init apps like this:像这样初始化应用程序:

apps: Array<SideNavItemModel> = [
  {
    title: "sideNav.centralWeighingLink",
    url: "centralWeighing",
    id: "1",
    icon: "mdi-scale-balance",
    isAuthorized: false
  },
  {
    title: "sideNav.appsLink",
    url: "apps",
    id: "2",
    icon: "mdi-apps",
    isAuthorized: true
  },
  {
    title: "sideNav.2Link",
    url: "password",
    id: "3",
    icon: "mdi-key-variant",
    isAuthorized: true
  },
]; 

And then render the list like this:然后像这样渲染列表:

 <v-list dense>
    <v-list-item v-for="app in apps" :key="app.id"
                 active-class="router-link-active" :to="`/${app.url}`" link>
      <div v-if="app.isAuthorized">
        <v-list-item-icon>
          <v-icon>{{ app.icon }}</v-icon>
        </v-list-item-icon>
        <v-list-item-content>
          <v-list-item-title>{{ $t(app.title) }}</v-list-item-title>
        </v-list-item-content>
      </div>
    </v-list-item>
  </v-list>

After user has logged, just need to set the first app's isAuthorized as true .用户登录后,只需将第一个应用的isAuthorized设置为true

Just saying my thoughts, hope it helps.只是说说我的想法,希望对你有帮助。

After the user logged in, store the token in the store with mutations.用户登录后,将令牌存储在带有突变的商店中。 Note that it will be a string but you should convert it into a boolean using !!请注意,它将是一个字符串,但您应该使用!!将其转换为 boolean ! . . '' = false but ' ' is true . '' = false' ' is true
This is all you need.这就是你所需要的。

 new Vue({ el: '#app', data() { return { hello: 'hellasdfo', token: false, } }, created() { // store in store and get it with getters // this.token =..this.$store.getters['token'] or..this;$store.getters.token this.token = !!'some token'; } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <p>Not LoggedIn</p> <p>Not LoggedIn</p> <p v-if="token">LoggedIn</p> </div>

As you are assigning isAuthorized property in each object in an array and then based on the $auth.user you are setting it as true / false .当您在数组中的每个 object 中分配isAuthorized属性时,然后根据$auth.user您将其设置为true / false Then we can simply achieve that by updating the flag in the created() hook.然后我们可以通过更新created()钩子中的标志来简单地实现这一点。

Live Demo :现场演示

 new Vue({ el: '#app', data: { apps: [ { title: "sideNav.centralWeighingLink", url: "centralWeighing", id: "1", icon: "mdi-scale-balance" }, { title: "sideNav.appsLink", url: "apps", id: "2", icon: "mdi-apps" }, { title: "sideNav.2Link", url: "password", id: "3", icon: "mdi-key-variant" } ], isLoggedInUser: false }, created() { if (this.isLoggedInUser) { this.apps.forEach(obj => obj.isAuthorized = true) } else { this.apps.forEach((obj, index) => { obj.isAuthorized = (index)? true: false }) } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div v-for="app in apps":key="app.id"> <li v-if="app.isAuthorized">{{ app.url }}</li> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM