首页 > 其他分享 >vue3使用vue-router构建SPA

vue3使用vue-router构建SPA

时间:2022-12-28 15:47:07浏览次数:48  
标签:npm vue import vue3 axios router SPA pages

使用自动化构建工具vite搭建新项目

#某个目录下执行
npm create vite@latest

 按照提示初始化项目,并按照提示:

cd vite-project
npm install
npm run dev

生成目录结构如下:

  安装常用的类库

npm install vue-router --save 
npm install axios --save 
npm install bootstrap --save

定义路由

新建src/router/index.js

import { createRouter, createWebHistory } from "vue-router";
import Home from "../pages/Home.vue";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {
            path: "/",
            name: "home",
            component: Home,
        },
        {
            path: "/reg",
            name: "reg",
            component: () => import("../pages/Reg.vue"),
        },
        {
            path: "/login",
            name: "login",
            component: () => import("../pages/Login.vue"),
        },
    ],
});
export default router;

 修改main.js

import router from "./router";

createApp(App).use(router).mount("#app");

 使用vue-router组件

Vue Router 是 Vue.js 的官方路由。它与 Vue.js 核心深度集成,让用 Vue.js 构建单页应用变得轻而易举。

修改App.vue

<script setup>
import "bootstrap/dist/css/bootstrap.min.css";
import { RouterLink, RouterView } from "vue-router";
</script>

<template>
  <div class="container-fluid">
    <nav class="navbar navbar-expand-lg navbar-light bg-light rounded" aria-label="Eleventh navbar example">
      <div class="container-fluid">
        <a class="navbar-brand" href="#">LOGO</a>

        <div class="collapse navbar-collapse">
          <ul class="navbar-nav me-auto mb-2 mb-lg-0">
            <li class="nav-item">
              <RouterLink to="/" class="nav-link active">Home</RouterLink>
            </li>
          </ul>

          <ul class="navbar-nav ms-auto mb-2">
            <li class="nav-item">
              <RouterLink to="/reg" class="nav-link ms-3">Register</RouterLink>
            </li>
            <li class="nav-item">
              <RouterLink to="/login" class="nav-link ms-3">Login</RouterLink>
            </li>
          </ul>
        </div>
      </div>
    </nav>
    <RouterView />
  </div>
</template>
<style>
a {
  cursor: pointer;
}
</style>

 src/pages/Home.vue

<template>
  <div class="about">
    <h1>This is home page</h1>
  </div>
</template>

 src/pages/Login.vue

<script setup>
import axios from "axios";
function login() {
  const formData = new FormData(document.getElementById("loginForm"));
  axios.post("/login", formData).then(function (response) {
    
  });
}
</script>
<template>
  <div >
    <div class="row">
      <div class="col-8 mx-auto" style="margin-top: 4rem">
        <div class="card p-5">
          <form id="loginForm">
            <div class="form-floating mb-3">
              <input type="text" class="form-control" id="floatingInput" name="email" placeholder="Email" />
              <label for="floatingInput">Email</label>
            </div>
            <div class="form-floating">
              <input type="password" class="form-control" id="floatingPassword" name="password" placeholder="Password" />
              <label for="floatingPassword">Password</label>
            </div>
            <div class="mt-3 text-center">
              <input type="button" class="btn btn-primary" value="login" v-on:click="login()" />
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

 src/pages/Reg.vue

<script setup>
import axios from "axios";
function reg() {
  const formData = new FormData(document.getElementById("regForm"));
    axios.post("/register", formData).then(function (response) {
      
    });
 }
</script>
<template>
  <div >
    <div class="row">
      <div class="col-8 mx-auto" style="margin-top: 10rem">
        <div class="card p-5">
          <form id="regForm">
            <div class="form-floating mb-3">
              <input type="text" class="form-control" id="floatingInput" name="name" placeholder="Name" required />
              <label for="floatingInput">Name</label>
            </div>
            <div class="form-floating mb-3">
              <input type="eamil" class="form-control" id="floatingInput" name="email" placeholder="Email" required />
              <label for="floatingInput">Email</label>
            </div>
            <div class="form-floating mb-3">
              <input type="password" class="form-control" id="floatingPassword" name="password" placeholder="Password" required />
              <label for="floatingPassword">Password</label>
            </div>
            <div class="form-floating mb-3">
              <input type="password" class="form-control" id="floatingPassword" name="password_confirmation" placeholder="password confirmation" required />
              <label for="floatingPassword">Password</label>
            </div>
            <div class="input-group text-center">
              <input type="button" class="btn btn-primary" value="register" v-on:click="reg()" />
              <a class="ms-3" href="index.html">已有账号?去登录</a>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

 

标签:npm,vue,import,vue3,axios,router,SPA,pages
From: https://www.cnblogs.com/caroline2016/p/17010199.html

相关文章

  • 基于图形化界面的方式创建vue2项目
    前提是安装vue-clinpmi-g@vue/cli接下来使用vueui创建vue项目vueui浏览器自动打开点击创建,然后去到你想把创建的项目放到的位置如果到不了那个位置,就......
  • jupyter notebook中运行pyspark代码
    前提是windows下安装pyspark​​设置连接​​用jupyternotebook编写pyspark代码frompyspark.sqlimportSparkSession#环境配置spark=SparkSession.builder.master("lo......
  • vue嵌入iframe场景键盘事件不触发的问题
    使用鼠标移动事件触发函数<iframe**@mouseover="setFocus()"**id="iframeUE4"frameborder="0"src="路径地址"></iframe>再调用内......
  • Vue3源码阅读梳理
    简单代码例子const{createApp,defineComponent,computed,watch,ref,reactive,effect}=Vueconstapp=createApp({components:[],template:`<div......
  • Vue3+vant+ts 上滑加载,解决上滑调用多次数据的问题
    之前用vue2的时候,写过vue2的用法,链接在这里点击跳转哈,用得挺好的,也没啥问题,照葫芦画瓢的做出来了,但是有问题,下滑之后调用多次数据,按理说组件通过 loading 和 finished......
  • vue环境配置文件详解
    一.什么是配置文件1.在vue项目目录下,我们可以看到诸如package.json、.gitignore、package-lock.json等等能配置项目的结构、引用的库、运行的方式、版本控制等等的都称为......
  • vue首页缓存
    vue.config.js添加chainWebpack:config=>{        config.plugins.delete('prefetch');//取消预加载    config.plugins.delete('preload......
  • react和vue中为什么用key、并且用key的时候为什么不能用index
    为什么用key在虚拟dom进行diff算法的时候,使用key可以对key进行比较然后来判断两个节点是否是同一节点,极大的增加了速度。为什么避免使用index先来总结好了的:如果只是......
  • vue 使用路由component: () =>import (‘ ‘)报错解决办法
    今天帮朋友调代码的时候,在人家的mac上面,项目没有任何错误,到我这里就出现component:()=>import(’')加载路由错误。发现是import处报错,import属于异步引用组件,需要......
  • vue 实现通过字符串关键字符动态渲染 input 输入框
    vue实现通过字符串关键字符动态渲染input输入框今天做一个简单的demo,就是有一个字符串,字符串里面有标识符,前端检测到标识符之后,需要将这个标识符转换成一个input输入框......