我有一个带有侧导航栏的单页应用程序,它在列表中有一组锚标记。
我想根据selectedPage
变量的值显示页面中显示的内容。以及根据侧栏中单击的链接更改selectedPage
的值。
即使在click事件中包含.prevent,下面的代码也不会更改变量的值。我应该使用另一种情况吗
的mypage.html
#Navbar for selecting content
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<li><a href="#" @click.prevent="selectedPage ='Foo'">Foo</a></li>
<li><a href="#" @click.prevent="selectedPage ='Bar'">Bar</a></li>
</ul>
</div>
#Page content
<div id="page-content-wrapper">
<div class="main-content" id="app" v-cloak>
<div class="container-fluid" v-if="selectedPage === 'Foo'">
<h3 class="page-title">{{selectedPage}}</h3>
</div>
<div class="container-fluid" v-if="selectedPage === 'Bar'">
<h3 class="page-title">{{selectedPage}}</h3>
</div>
</div>
</div>
App.js
new Vue({
el: '#app',
data: {
selectedPage: 'Foo',
}
})
答案 0 :(得分:7)
这符合我的预期;你的工作方式如何?
new Vue({
el: '#app',
data: {
selectedPage: 'Foo'
}
});
#app {
display: flex;
}
#sidebar-wrapper {
border-right: solid thin black;
margin-right: 15px;
padding-right: 15px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<div id="app">
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<li><a href="#" @click.prevent="selectedPage ='Foo'">Foo</a></li>
<li><a href="#" @click.prevent="selectedPage ='Bar'">Bar</a></li>
</ul>
</div>
#Page content
<div id="page-content-wrapper">
<div class="main-content" id="app" v-cloak>
<div class="container-fluid" v-if="selectedPage === 'Foo'">
<h3 class="page-title">{{selectedPage}}</h3>
Foo section
</div>
<div class="container-fluid" v-if="selectedPage === 'Bar'">
<h3 class="page-title">{{selectedPage}}</h3>
Bar section
</div>
</div>
</div>
</div>
答案 1 :(得分:0)
我只是遇到了一个有趣的场景,我只使a11y的按钮和键盘一起工作。
我做到了,效果很好:
单击该按钮将通过vue-popperjs
加载一个弹出窗口,此后在单击菜单项时需要将其关闭。我将展示整个标记并在之后进行讨论:
<template>
<v-popper trigger="click" :options="{ placement: 'bottom' }" enter-active-class="shadow-lg">
<div class="popper">
<div class="flex flex-col text-left p-2">
<span class="text-pink font-bold py-2 px-8">Download</span>
<a
:href="defaultAction"
class="no-underline text-grey-darkest hover:bg-pink-lighter py-2 px-8"
@click="$refs.downloadButton.click()"
>
CSV
</a>
<a
:href="urlsAction"
class="no-underline text-grey-darkest hover:bg-pink-lighter py-2 px-8"
@click="$refs.downloadButton.click()"
>
CSV with URLs
</a>
</div>
</div>
<button
ref="downloadButton"
slot="reference"
class="flex items-center justify-center text-center no-underline rounded-full w-8 h-8 text-grey-darker hover:text-pink active:text-pink-darker mr-2"
title="Download CSV"
>
<i class="fas fa-download"></i>
</button>
</v-popper>
</template>
这里有两个关键的事情:
在按钮上放置ref
点击$ref.downloadButton.click()
标签后触发<a>
当单击菜单项时,这将关闭弹出窗口,为您带来一些不适的UX。