如何使用 Vue-Router 在 VueJS 中重定向页面

发布于 2025-01-16 16:29:13 字数 3089 浏览 0 评论 0原文

我正在尝试从我的登录模板重定向

<template>
  <formulario-login/>
</template>

<script>
import formularioLogin from "@/components/formularioLogin";

export default {
  name: "index",
  components: {
    formularioLogin
  },
  methods: {

  }
}
</script>

<style scoped>

</style>

<template>
  <div class="center">
      Login
      <input type="text" v-model="usuario.login">
      <br/>
      Senha
      <input type="password" v-model="usuario.password">
      <br/>
      <button @click="logar"> Login</button>

  </div>

</template>

<script>
import axios from "axios";

export default {
  name: "barraInicial",
  data: () => {
    return {
      usuario: {
        login: undefined,
        password: undefined
      }
    }
  },
  methods: {
    async logar() {
      let formData = new FormData();
      formData.append("user", this.usuario)
      await axios.post('http://localhost:8080/login', this.usuario)
          .then(res => {
            if(res.data === true){
              this.$router.push('/App')
            }
          })
          .catch(res => console.log(res))
    }
  }
}
</script>

<style scoped>
.center {
  margin: auto;
  width: 50%;
  border: 3px solid green;
  padding: 10px;
}
</style>

到我的应用程序模板

<template>
  <div class="container">
    <div class="large-12 medium-12 small-12 cell">
      <label>Select file
        <input type="text" v-model="nome"/>
        <input type="file" id="file" v-on:change="handleFileUpload"/>
      </label>
      <button v-on:click="submitFile">Send</button>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'App',
  components: {
  },
  data: () => ({
    file: '',
    nome: ''
  }),
  methods: {
    handleFileUpload( event ){
      this.file = event.target.files[0];
    },
    async submitFile(){
      let formData = new FormData();
      formData.append("image", this.file, this.file.name);
      formData.append("nome", this.nome)
      await axios.post( 'http://localhost:8080/image.do',
          formData,

      )
    },
  }
}
</script>

<style>
*{
}
</style>

我已经在我的项目和我的 main.js 中添加了一个 Vue-Router

import { h } from 'vue'
import App from './App.vue'
import Index from './Index.vue'
import * as VueRouter from 'vue-router'
import * as Vue from 'vue'

const routes = [
    {path: '/', component: Index, name: 'index'},
    {path: '/App', component: App, name: 'program'},
]

const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes,
})

const app = Vue.createApp({
    render: ()=>h(Index)
})
app.use(router)
app.mount('#app')

我已经创建了一个 Vue 3 项目,并且我正在尝试从我的模板登录重定向在我的应用程序模板中,我将 Vue-Router 添加到我的项目中并创建了模板的路径,但是当我的前端从后端接收到布尔值 true 时,它​​们会进入 if 条件,URL 发生变化,但模板不会发生变化t,该控制台也不显示任何内容

I'm tring to redirect from my Login Template

<template>
  <formulario-login/>
</template>

<script>
import formularioLogin from "@/components/formularioLogin";

export default {
  name: "index",
  components: {
    formularioLogin
  },
  methods: {

  }
}
</script>

<style scoped>

</style>

<template>
  <div class="center">
      Login
      <input type="text" v-model="usuario.login">
      <br/>
      Senha
      <input type="password" v-model="usuario.password">
      <br/>
      <button @click="logar"> Login</button>

  </div>

</template>

<script>
import axios from "axios";

export default {
  name: "barraInicial",
  data: () => {
    return {
      usuario: {
        login: undefined,
        password: undefined
      }
    }
  },
  methods: {
    async logar() {
      let formData = new FormData();
      formData.append("user", this.usuario)
      await axios.post('http://localhost:8080/login', this.usuario)
          .then(res => {
            if(res.data === true){
              this.$router.push('/App')
            }
          })
          .catch(res => console.log(res))
    }
  }
}
</script>

<style scoped>
.center {
  margin: auto;
  width: 50%;
  border: 3px solid green;
  padding: 10px;
}
</style>

To my App Template

<template>
  <div class="container">
    <div class="large-12 medium-12 small-12 cell">
      <label>Select file
        <input type="text" v-model="nome"/>
        <input type="file" id="file" v-on:change="handleFileUpload"/>
      </label>
      <button v-on:click="submitFile">Send</button>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'App',
  components: {
  },
  data: () => ({
    file: '',
    nome: ''
  }),
  methods: {
    handleFileUpload( event ){
      this.file = event.target.files[0];
    },
    async submitFile(){
      let formData = new FormData();
      formData.append("image", this.file, this.file.name);
      formData.append("nome", this.nome)
      await axios.post( 'http://localhost:8080/image.do',
          formData,

      )
    },
  }
}
</script>

<style>
*{
}
</style>

I've added a Vue-Router into my project and my main.js

import { h } from 'vue'
import App from './App.vue'
import Index from './Index.vue'
import * as VueRouter from 'vue-router'
import * as Vue from 'vue'

const routes = [
    {path: '/', component: Index, name: 'index'},
    {path: '/App', component: App, name: 'program'},
]

const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes,
})

const app = Vue.createApp({
    render: ()=>h(Index)
})
app.use(router)
app.mount('#app')

I've created a Vue 3 project, and i'm trying to redirect from my Template Login to my App Template, i added Vue-Router into my project and created path's to my templates, but when my front-end receive a boolean true from my back-end, they step into if condition, the URL change, but template don't, the console don't show nothing too

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹短裙飘 2025-01-23 16:29:13

由于您没有包含 Index.vue 文件,我认为您忘记了

参考

// Index.vue
<template>
  <router-view />
</template>

<script>
export default {
  name: "Index",
}
</script>

Since you not including Index.vue file, I think you forgot the <router-view/>

Reference

// Index.vue
<template>
  <router-view />
</template>

<script>
export default {
  name: "Index",
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文