Nuxt的路由动画效果案例
路由的动画效果,也叫作页面的更换效果。Nuxt.js提动两种方法为路由提动动画效果,一种是全局的,一种是针对单独页面制作。

全局路由动画
全局动画默认使用page进行设置,例如现在我们为每个页面都设置一个进入和退出时的渐隐渐现的效果。我们可以先在根目录的assets/css下建立一个main.css文件。
/assets/css/main.css
.page-enter-active,.page-leave-active{
transition: opacity 2s;
}
.page-enter,.page-leave-active{
opacity: 0;
}
然后在nuxt.config.js里加入一个全局的css文件就可以了。
module.exports = {
/*
** Headers of the page
*/
head: {
title: 'delnuxt',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: 'Nuxt.js project' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
],
},
css:['~assets/css/normailze.css','~assets/css/main.css'],
/*
** Customize the progress bar color
*/
loading: { color: '#3B8070' },
/*
** Build configuration
*/
build: {
/*
** Run ESLint on save
*/
extend (config, { isDev, isClient }) {
if (isDev && isClient) {
config.module.rules.push({
enforce: 'pre',
test: /\.(js|vue)$/,
loader: 'eslint-loader',
exclude: /(node_modules)/
})
}
}
}
}
这时候在页面切换的时候就会有2秒钟的动画效果了,但是你会发现一些页面时没有效果,这是因为你没有使用<nuxt-link>组件来制作跳转链接。你需要进更改。
比如改成如下:
<nuxt-link :to="{name:'news',params:{newsId:3306}}">NEWS</nuxt-link>
改过之后你就会看到有动画效果了。
单独设置页面动效
想给一个页面单独设置特殊的效果时,我们只要在css里改变默认的page,然后在页面组件的配置中加入transition字段即可。例如,我们想给about页面加入一个字体放大然后缩小的效果,其它页面没有这个效果。
在全局样式assets/main.css中添加以下内容。
.test-enter-active,.test-leave-active{
transition: all 2s;
font-size: 12px;
}
.test-enter,.test-leave-active{
opacity: 0;
font-size: 40px;
}
然后在about/index.vue组件中设置
<script>
export default {
transition:'test'
}
</script>
补充知识:vue-ssr框架nuxt填坑
Nuxt.js 1.0.0 初始化与依赖包安装
vue init nuxt/started
npm install
npm run dev
npm run dev 报错
> nuxt-temp@1.0.0 dev E:\MaYunProject\nuxt-temp
> nuxt
E:\MaYunProject\nuxt-temp\node_modules\nuxt\dist\nuxt.js:79
async function promiseFinally(fn, finalFn) {
^^^^^^^^
SyntaxError: Unexpected token function
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (E:\MaYunProject\nuxt-temp\node_modules\nuxt\index.js:17:20)
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "G:\\node\\node.exe" "G:\\node\\node_modules\\npm\\bin\\npm-cli.js" "run" "dev"
npm ERR! node v6.11.4
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE
npm ERR! nuxt-temp@1.0.0 dev: `nuxt`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the nuxt-temp@1.0.0 dev script 'nuxt'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the nuxt-temp package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! nuxt
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs nuxt-temp
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls nuxt-temp
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! E:\MaYunProject\nuxt-temp\npm-debug.log
解决错误
将node 升级到 node8.12.0
升级到nuxt-edge Nuxt.js 2.0
1、运行 npm run dev报错
ERROR Failed to compile with 1 errors Module build failed (from ./node_modules/eslint-loader/index.js): TypeError: Cannot read property 'eslint' of undefined at Object.module.exports (.../node_modules/eslint-loader/index.js:148:18) You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.
2、修正错误:编辑nuxt.conf.js文件并将其更改为
- module.exports = {
+ export default {
// ...
build: {
/*
** Run ESLint on save
*/
- extend (config, { isDev, isClient }) {
- if (isDev && isClient) {
+ extend (config, { isDev }) {
+ if (isDev && process.client) {
config.module.rules.push({
enforce: 'pre',
test: /\.(js|vue)$/,
loader: 'eslint-loader',
exclude: /(node_modules)/
})
}
}
}
}
3、 重启服务,打开浏览器并访问:http://localhost:3000/。
以上这篇Nuxt的路由动画效果案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
代码知识SEO上一篇 : Python常用base64 md5 aes des crc32加密解密方法汇总
下一篇 : python如何编写类似nmap的扫描工具
-
SEO外包最佳选择国内专业的白帽SEO机构,熟知搜索算法,各行业企业站优化策略!
SEO公司
-
可定制SEO优化套餐基于整站优化与品牌搜索展现,定制个性化营销推广方案!
SEO套餐
-
SEO入门教程多年积累SEO实战案例,从新手到专家,从入门到精通,海量的SEO学习资料!
SEO教程
-
SEO项目资源高质量SEO项目资源,稀缺性外链,优质文案代写,老域名提权,云主机相关配置折扣!
SEO资源
-
SEO快速建站快速搭建符合搜索引擎友好的企业网站,协助备案,域名选择,服务器配置等相关服务!
SEO建站
-
快速搜索引擎优化建议没有任何SEO机构,可以承诺搜索引擎排名的具体位置,如果有,那么请您多注意!专业的SEO机构,一般情况下只能确保目标关键词进入到首页或者前几页,如果您有相关问题,欢迎咨询!