最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to open a new page on button click in vue 3? - Stack Overflow

programmeradmin4浏览0评论

I have this application with a button and I want to change to a different, Vue file. My first Vue file is called app.vue.

<template>
  <div id="app">
    <button id="gettingStarted">Getting started</button>
  </div>
</template>

<script>

export default {
  name: 'App',
  ponents: {

  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
#gettingStarted {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  background:cornflowerblue;
  border-radius: 0.5rem;
  height: 2.5rem;
  width: 6%;
}
</style>

and then I have another file called Gallery.vue

<template>
  <div id="app">
      <h4>25%</h4>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
  ponents: {
    
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

So on the button click in-app.vue I want to go to gallery.vue. I am currently using Vue3 for this project.

I have this application with a button and I want to change to a different, Vue file. My first Vue file is called app.vue.

<template>
  <div id="app">
    <button id="gettingStarted">Getting started</button>
  </div>
</template>

<script>

export default {
  name: 'App',
  ponents: {

  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
#gettingStarted {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  background:cornflowerblue;
  border-radius: 0.5rem;
  height: 2.5rem;
  width: 6%;
}
</style>

and then I have another file called Gallery.vue

<template>
  <div id="app">
      <h4>25%</h4>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',
  ponents: {
    
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

So on the button click in-app.vue I want to go to gallery.vue. I am currently using Vue3 for this project.

Share Improve this question asked Sep 28, 2020 at 0:54 user7200977user7200977 1
  • Is this part of a SPA? – Jaromanda X Commented Sep 28, 2020 at 1:08
Add a ment  | 

4 Answers 4

Reset to default 5
<button id="gettingStarted" @click="click">Getting started</button>
...
import { useRouter } from 'vue-router'
export default {
    setup() {
        const router = useRouter()
        const click = () => {
            router.push({
                path: 'target path'
            })
        }
        return {
            click
        }
    }
}

You can always use Vue Router.

This tutorial will explain better than me :

https://appdividend./2018/12/28/vue-router-tutorial-with-example-how-to-use-routing-in-vuejs

essentially what you want to do is render different ponents

<Component1 v-if="useComponent1" />
<Component2 v-if="useComponent2" />

but that gets a bit unwieldly quite quickly ( though might be useful for something pretty simple. So quite often what people do is "client side" routing.

In Vue3, you can use the simple router in the docs https://v3.vuejs/guide/routing.html#simple-routing-from-scratch

or use something like VueRouter (which is pretty mon)

If you are using Laravel inertia you can route programatically like this

this.route('routename', [id, {page: page}])

Where id might be a url parameter and {page: page} is a query parameter.

发布评论

评论列表(0)

  1. 暂无评论