第四章路由动画与滚动效果
1.创建项目
bash
yarn create vite animation --template vue
cd animation
yarn
yarn add vue-router
yarn add -D sass
yarn dev
App.vue
vue
<template>
<div>
<router-view />
</div>
</template>
<script setup></script>
<style lang="scss"></style>
main.js
javascript
import router from "./router";
createApp(App).use(router).mount("#app");
router/index.js
javascript
import { createRouter, createWebHistory } from "vue-router";
const router = createRouter({
history: createWebHistory(),
routes: [{}],
});
export default router;
2.路由跳转与样式声明
vite.config.js
javascript
import path from "path"
export default defineConfig({
resolve:{alias:{'@',path.resolve(__dirname,'src')}}
})
router/index.js
javascript
import { createRouter, createWebHistory } from "vue-router";
import about from "@/views/about.vue";
import home from "@/views/home.vue";
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: "/home", name: "home", component: home },
{ path: "/about", name: "about", component: about },
],
});
export default router;
App.vue
vue
<template>
<div class="link">
<router-link :to="{ name: 'home' }">Home</router-link>
<router-link :to="{ name: 'about' }">About</router-link>
</div>
<div class="router-link">
<router-view />
</div>
</template>
<script setup></script>
<style lang="scss">
.link {
a {
display: inline-block;
background-color: #16a085;
color: #fff;
margin-right: 20px;
text-align: center;
padding: 10px;
}
}
.router-link {
margin-top: 20px;
}
</style>
main.js
javascript
import "./assets/global.scss";
assets/global.scss
css
body {
width: 100vw;
height: 100vh;
padding: 30px;
color: #666;
}
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
a {
text-decoration: none;
}