需要显示通知(v-alert通知),该通知将在所有页面上反映出来。

huangapple go评论71阅读模式
英文:

Need to displayed notification(v-alert notification) which will be reflecting on all page

问题

有一个情景,在右侧抽屉中有一个包含等级下拉列表的情况,其中显示了grade1、grade2、grade3等等级,所有页面上都需要显示v-alert通知条,显示学生的总数。例如,如果我从右侧抽屉中选择了grade3,那么v-alert通知条上将显示grade 3的总学生数。如果我在右侧抽屉中更改了下拉列表的值为grade1,那么所有页面上的v-alert上将反映出grade 1的学生总数。

注意 - v-alert通知条应该像导航栏一样显示在所有页面上。

我该如何实现这个功能?

我应该使用stores吗?

英文:

There is scenario where there is right drawer in which grade dropdown with list grade1, grade2, grade3 etc is been displayed and on all pages I need v-alert notification strip to display total count of student. So, for example from right drawer if I select grade3 then on v-alert strip total student of grade 3 will be displayed. If I change the dropdown value in right drawer to grade1 then total number of student for grade 1 will be reflected on v-alert on all pages.

NOTE - v-alert strip should be displayed on all pages just like navbar.

How can I achieve this?

Should I use stores for this?

答案1

得分: 0

我不确定你的组件结构,但我假设你在主页面(父页面)中同时拥有drawerv-alert,其他路由在其内加载。如果我的理解是正确的,你可以尝试这个:

<!-- language: lang-html -->
<div id="app">
  <v-app id="inspire">
    <v-card height="400" width="256" class="mx-auto">
      <v-alert border="left" color="indigo" dark>
        Count : {{ studentCount }}
      </v-alert>
      <child @student-count="getStudentCount"></child>
    </v-card>
  </v-app>
</div>
Vue.component('child', {
  data() {
    return {
      items: [
        { title: 'Grade 1', icon: 'mdi-view-dashboard', count: 5 },
        { title: 'Grade 2', icon: 'mdi-view-dashboard', count: 10 },
        { title: 'Grade 3', icon: 'mdi-view-dashboard', count: 15 },
      ],
    }
  },
  template: `<v-navigation-drawer permanent>
                  <v-divider></v-divider>
                  <v-list dense nav>
                    <v-list-item v-for="item in items" :key="item.title" link @click="getStudentCount(item.title)">
                      <v-list-item-icon>
                        <v-icon>{{ item.icon }}</v-icon>
                      </v-list-item-icon>
                      <v-list-item-content>
                        <v-list-item-title>{{ item.title }}</v-list-item-title>
                      </v-list-item-content>
                    </v-list-item>
                  </v-list>
                </v-navigation-drawer>`,
  methods: {
    getStudentCount(title) {
      this.$emit('student-count', this.items.find(obj => obj.title === title).count)
    }
  }          
});

var app = new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      right: null,
      studentCount: 0
    }
  },
  methods: {
    getStudentCount(e) {
      this.studentCount = e
    }
  }
});

以上是你提供的代码的翻译部分。

英文:

I am not sure about the component structure you have but I am demoing based on the assumption that you have both drawer and v-alert in a master page (parent page) and other routes are loading inside it. If my understanding is correct. You can give a try to this :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

Vue.component(&#39;child&#39;, {
  data() {
    return {
      items: [
        { title: &#39;Grade 1&#39;, icon: &#39;mdi-view-dashboard&#39;, count: 5 },
        { title: &#39;Grade 2&#39;, icon: &#39;mdi-view-dashboard&#39;, count: 10 },
        { title: &#39;Grade 3&#39;, icon: &#39;mdi-view-dashboard&#39;, count: 15 },
      ],
    }
  },
  template: `&lt;v-navigation-drawer permanent&gt;
              &lt;v-divider&gt;&lt;/v-divider&gt;
              &lt;v-list dense nav&gt;
                &lt;v-list-item v-for=&quot;item in items&quot; :key=&quot;item.title&quot; link @click=&quot;getStudentCount(item.title)&quot;&gt;
                  &lt;v-list-item-icon&gt;
                    &lt;v-icon&gt;{{ item.icon }}&lt;/v-icon&gt;
                  &lt;/v-list-item-icon&gt;
                  &lt;v-list-item-content&gt;
                  	&lt;v-list-item-title&gt;{{ item.title }}&lt;/v-list-item-title&gt;
                  &lt;/v-list-item-content&gt;
                &lt;/v-list-item&gt;
              &lt;/v-list&gt;
            &lt;/v-navigation-drawer&gt;`,
  methods: {
  	getStudentCount(title) {
      this.$emit(&#39;student-count&#39;, this.items.find(obj =&gt; obj.title === title).count)
    }
  }          
});

var app = new Vue({
  el: &#39;#app&#39;,
  vuetify: new Vuetify(),
  data () {
    return {
      right: null,
      studentCount: 0
    }
  },
  methods: {
  	getStudentCount(e) {
    	this.studentCount = e
    }
  }
});

<!-- language: lang-html -->

&lt;script src=&quot;https://unpkg.com/vue@2.x/dist/vue.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://unpkg.com/vuetify@2.6.13/dist/vuetify.min.js&quot;&gt;&lt;/script&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/vuetify@2.6.13/dist/vuetify.min.css&quot;/&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://fonts.googleapis.com/css?family=Material+Icons&quot;/&gt;

&lt;div id=&quot;app&quot;&gt;
  &lt;v-app id=&quot;inspire&quot;&gt;
    &lt;v-card height=&quot;400&quot; width=&quot;256&quot; class=&quot;mx-auto&quot;&gt;
      &lt;v-alert border=&quot;left&quot; color=&quot;indigo&quot; dark&gt;
        Count : {{ studentCount }}
      &lt;/v-alert&gt;
      &lt;child @student-count=&quot;getStudentCount&quot;&gt;&lt;/child&gt;
    &lt;/v-card&gt;
  &lt;/v-app&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 15:45:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定