当前位置: 移动技术网 > IT编程>脚本编程>Python > Nuxt的路由动画效果案例

Nuxt的路由动画效果案例

2020年11月06日  | 移动技术网IT编程  | 我要评论
路由的动画效果,也叫作页面的更换效果。nuxt.js提动两种方法为路由提动动画效果,一种是全局的,一种是针对单独页面制作。全局路由动画全局动画默认使用page进行设置,例如现在我们为每个页面都设置一个

路由的动画效果,也叫作页面的更换效果。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的路由动画效果案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网