如何使用 Vue-Router 在 VueJS 中重定向页面
我正在尝试从我的登录模板重定向
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您没有包含
Index.vue
文件,我认为您忘记了参考
Since you not including
Index.vue
file, I think you forgot the<router-view/>
Reference