英文:
Сhanging number of buttons with different functions in Header-component
问题
我想要一个具有不同数量的按钮和这些按钮可以触发不同功能的 Header 组件,例如,在主页上是 "访问关于页面" 和 "触发 Vuex 动作 A",在关于页面上是 "访问主页" 和 "触发 Vuex 动作 B"。在Vue中实现这个的正确方法是什么?
英文:
I would like to have a Header-component but with varying number of buttons and different functions that those buttons can trigger, for example, "visit about page", "trigger vuex action A" on home page and "visit home page", "trigger vuex action B" on about page.
What is the right way to achieve this in Vue?
答案1
得分: 0
以下是已翻译的内容:
这是一个使用Vue [组件][1]、[插槽][2]和[Semantic UI][3]的非常基本的示例。
<my-menu>
<menu-item header>
<a href="#">Home</a>
</menu-item>
<menu-item @click="gotoAboutUs()">
About Us
</menu-item>
</my-menu>
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const { createApp, ref } = Vue
const MyMenu = {
template: `<div class="ui menu"><slot></slot></div>`
}
const MenuItem = {
props: {
header: { type: Boolean },
right: { type: Boolean }
},
template: `<div :class="['item', header ? 'header' : '', right ? 'right' : '']"><slot></slot></div>`
}
const App = {
components: {
MyMenu, MenuItem
},
methods: {
gotoAboutUs: () => alert('About Us')
}
}
const app = createApp(App)
app.mount('#app')
<!-- language: lang-css -->
#app { line-height: 1.75; }
[v-cloak] { display: none; }
<!-- language: lang-html -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.5.0/dist/semantic.min.css">
<div id="app" v-cloak>
<my-menu>
<menu-item header>
<a href="#">Home</a>
</menu-item>
<menu-item @click="gotoAboutUs()">
About Us
</menu-item>
<menu-item right>
<div class="ui action input">
<input type="text" placeholder="Navigate to...">
<div class="ui button">Go</div>
</div>
</menu-item>
</my-menu>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<!-- end snippet -->
注意:以上内容已经翻译,不包括代码部分。
<details>
<summary>英文:</summary>
Here is a very basic example using Vue [Components][1], [Slots][2] and [Semantic UI][3].
<my-menu>
<menu-item header>
<a href="#">Home</a>
</menu-item>
<menu-item @click="gotoAboutUs()">
About Us
</menu-item>
</my-menu>
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const { createApp, ref } = Vue
const MyMenu = {
template: `<div class="ui menu"><slot></slot></div>`
}
const MenuItem = {
props: {
header: { type: Boolean },
right: { type: Boolean }
},
template: `<div :class="['item', header ? 'header' : '', right ? 'right' : '']"><slot></slot></div>`
}
const App = {
components: {
MyMenu, MenuItem
},
methods: {
gotoAboutUs: () => alert('About Us')
}
}
const app = createApp(App)
app.mount('#app')
<!-- language: lang-css -->
#app { line-height: 1.75; }
[v-cloak] { display: none; }
<!-- language: lang-html -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.5.0/dist/semantic.min.css">
<div id="app" v-cloak>
<my-menu>
<menu-item header>
<a href="#">Home</a>
</menu-item>
<menu-item @click="gotoAboutUs()">
About Us
</menu-item>
<menu-item right>
<div class="ui action input">
<input type="text" placeholder="Navigate to...">
<div class="ui button">Go</div>
</div>
</menu-item>
</my-menu>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<!-- end snippet -->
[1]: https://vuejs.org/guide/essentials/component-basics.html
[2]: https://vuejs.org/guide/components/slots.html
[3]: https://semantic-ui.com/
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论