简体   繁体   English

如何在导出默认值中执行 if

[英]how to do a if in a export default

ima real newbie in web programming and i try to do a question-reponse in vue.js我是 web 编程中的真正新手,我尝试在 vue.js 中进行问答

export default {
 // I want to an if (character == "Passé" then do this steps")
  "steps": [

    {
      
      "id": 1,
      "background": jpg.village,
      "title": "Votre aventure épique commence ici. Pour découvrir la raison de ces terribles disparitions, il faudrait commencer à enquêter au plus vite. Cependant, vous vous apprêtiez à aller a la boutique pour acheter des équipements qui pourraient peut-être vous aider pour votre quête...",
      "actions": [
        {
          "description": "Aller à la boutique d'équipements",
          "path": 2
        },

 "characters": [
    {
      "class": "Passé",
      "description": "“ A l'antiquité, la puissance ecrasait la défense. Attaquez et vous survivrez”",
      "stats": {
        "luck": 5,
        "power": 20,
        "agility": 4,
        "health": 85
      }
    },
    {
      "class": "Present",
      "description": "“ Aujourd'hui, nous sommes dans une ère ou la puissance et la defense sont egales, le seul frein.. La mobilité. ”",
      "stats": {
        "luck": 4,
        "power": 10,
        "agility": 2,
        "health": 150
      }
    },

    }
  ]

but he doesn't like it, i saw other tutoriels but they never use it in a "export defaut"但他不喜欢它,我看过其他教程,但他们从未在“出口默认值”中使用它

Thanks:)谢谢:)

Most likely you are importing it in another module using curly brackets.很可能您正在使用大括号将其导入另一个模块。 When default exporting, it should be imported without curly brackets.默认导出时,应在不带大括号的情况下导入。

Wrong:错误的:

import {something} from someModule

Right:正确的:

import something from someModule

You can do this to add inline optional properties to an object:您可以这样做以将内联可选属性添加到 object:

export default {
  ...(character !== 'Passé' ? {} : { steps: [<your steps array>] })
}

The ... is called a spread operator. ...称为扩展运算符。 It will expand the properties of the variable attached to it into the parent object (also works for arrays).它将附加到它的变量的属性扩展为父 object(也适用于数组)。 In this case, we are adding either {} (nothing) or { steps: [] } based on your condition.在这种情况下,我们将根据您的情况添加{} (无)或{ steps: [] }

You can also do steps: character?== 'Passé': [] : [ <steps array> ]您还可以执行steps: character?== 'Passé': [] : [ <steps array> ]
This will make it so that steps will always be defined on your object as an array.这将使steps始终在 object 上定义为数组。

Both of these will work.这两种方法都会起作用。

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

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