带锚标签的VueJS @click

exc*_*ion 7 javascript vue.js vuejs2

我有一个带有侧导航栏的单页面应用程序,它在列表中有一组锚标记.

我想根据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>
Run Code Online (Sandbox Code Playgroud)

App.js

new Vue({
  el: '#app',
  data: {
    selectedPage: 'Foo',
  }
})
Run Code Online (Sandbox Code Playgroud)

Roy*_*y J 11

这符合我的预期; 你的工作方式如何?

new Vue({
  el: '#app',
  data: {
    selectedPage: 'Foo'
  }
});
Run Code Online (Sandbox Code Playgroud)
#app {
  display: flex;
}

#sidebar-wrapper {
  border-right: solid thin black;
  margin-right: 15px;
  padding-right: 15px;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)