简体   繁体   English

Nuxt 页面作为组件?

[英]Nuxt pages as components?

I am new to Nuxt but not to Vue.我是 Nuxt 的新手,但不是 Vue 的新手。 My understanding is that Nuxt pages are components.我的理解是 Nuxt 页面是组件。 And after setting up a site with the Nuxt scaffolding, things like asyncData and computed properties work fine on the default index.vue file.在使用 Nuxt 脚手架设置站点后,asyncData 和计算属性之类的东西在默认的 index.vue 文件上可以正常工作。 But either this doesn't apply to pages in subdirectories, or I am missing how to do it.但这要么不适用于子目录中的页面,要么我错过了如何做到这一点。 If on a page /session/books.vue, if I do something like:如果在 /session/books.vue 页面上,如果我执行以下操作:

<template>{{ name }}, {{allBooks }}</template>

<script>export default {

   asyncData (context) {
    return {name: 'world'}
   },

   computed: {
    allBooks () {
      return this.$store.state.books.all
      // or even just return 'hello'
    }
  }
 }
</script>

I get "Uncaught SyntaxError: Unexpected token 'export'", plus a warning 'Property or method is not defined on the instance but referenced during render' for both 'name' and 'allBooks'.我收到“未捕获的 SyntaxError:意外的令牌 'export'”,以及“名称”和“allBooks”的警告“未在实例上定义属性或方法,但在渲染期间引用”。 Am I coding the script element incorrectly, or are there rules governing how the page components behave that I'm unaware of?我是否对脚本元素进行了错误的编码,或者是否有一些我不知道的管理页面组件行为的规则? Surely I don't need to register every page as a component?当然我不需要将每个页面都注册为组件吗? These all seem like very basic things, but I've googled both of these all morning, looked over the documentation, and haven't come up with anything helpful.这些看起来都是非常基本的东西,但是我整个早上都在谷歌上搜索了这两个,查看了文档,并没有想出任何有用的东西。

Your template section is not valid.您的模板部分无效。 Template section must be valid html and must contain only 1 root element.模板部分必须是有效的 html 并且必须仅包含 1 个根元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM