小编xx *_* yy的帖子

javascript:修改导入的“变量”会导致“分配给常量变量”,即使它不是常量

我有两个文件,file1 导出一个变量“不是常量” var x=1 和 file2 从中导入这个变量

问题是我无法修改导入的变量,即使它不是常量!

文件1.js

export var x=1 //it is defined as a variable not a constant
Run Code Online (Sandbox Code Playgroud)

文件2.js

import {x} from 'file1.js'
console.log(x) //1
x=2 //Error: Assignment to constant variable
Run Code Online (Sandbox Code Playgroud)

javascript node.js ecmascript-6 es6-module-loader es6-modules

7
推荐指数
1
解决办法
1168
查看次数

如何有条件地连接mongodb聚合中的字段

我想通过连接一些字段来构建一个字符串,但我想在决定连接它之前检查每个字段的值。

//syntax:
{$projection:{fieldName:{$concat:["$field1","-","$field2","$field3"]}}}
Run Code Online (Sandbox Code Playgroud)

我想要的是?

fieldName=($field1!=null?"$field1-":"")+"$field2"+($field3=="ok"?"approved":"pending")
Run Code Online (Sandbox Code Playgroud)

mongodb mongodb-query aggregation-framework

4
推荐指数
1
解决办法
3378
查看次数

Nuxt和vue中的Data()VS asyncData()

二者data()async data()给出了相同的结果(和显而易见的是,从结果asyncData()覆盖从结果data()

两者都会在源代码中生成HTML代码(即,在服务器端呈现的代码)

两者都可以用来await获取要提取的数据(例如:使用axios)

那么,它们之间有什么区别?

<template>
  <div>
    <div>test:  {{test}}</div>
    <div>test2: {{test2}}</div>
    <div>test2: {{test3}}</div>
    <div>test2: {{test4}}</div>
  </div>
</template>

<script>
export default {
    asyncData(app){
    return {test:"asyncData",test2:"asyncData2",test3:"asyncData3"}
  },data(){
    return {test:"data",test2:"data2",test4:"data4"}
 }
}
</script>
Run Code Online (Sandbox Code Playgroud)

结果:

test:  asyncData
test2: asyncData2
test2: asyncData3
test2: data4
Run Code Online (Sandbox Code Playgroud)

vue.js vuex vuejs2 nuxt.js nuxt

4
推荐指数
2
解决办法
7845
查看次数

当并非所有字段都有值时连接字段

我想通过聚合管道中的投影阶段修改一个字段,该字段是由(-)分隔的其他字段值的组合

如果该字段为空或为空或缺失,则不会将其添加到串联字符串中

{$project:{

//trial-1:
finalField:{
 $concat["$field1",'-','$field2','-','$field3',...] 
//problem1: $concat will return null if any of it's arguments is null or missing
//problem2: if all the fields are exist with non-null values, the delimiter will exists even if the field dosen't 
}


//trial-2:
finalField:{
 $concat:[
  {$cond:[{field1:null},'',{$concat:['$field1','-']},..]
//the problem: {field1:null} fails if the field dosen't exixt (i.e the expression gives true)

//trial-3
finalField:{
 $concat:[
  {$cond:[{$or:[{field1:null},{field:{$exists:true}},'',
   {$concat:['$field1','-']}
]}]}
]
}

] 
}

//trial-4 -> using $reduce instead of $concate (same issues)
Run Code Online (Sandbox Code Playgroud)

}

mongodb mongodb-query aggregation-framework

3
推荐指数
1
解决办法
3305
查看次数

从 NUXT 布局中的“assets”目录加载 css 文件

在nuxt布局(default.vue)中,我想从assets文件夹加载图像和css文件,图像加载成功,但css文件没有,为什么?

/layouts/default.vue

<template>
<img src="~assets/photo.jpg" />
 <!-- converted to /_nuxt/assets/photo.jpg and loaded successfully -->
</template>

<script>
export default{
 head:{
  link: [  { rel: 'stylesheet', href: '~assets/style.css' }]
}
}
</sript>
Run Code Online (Sandbox Code Playgroud)

当我查看源代码时:

<link href="~assets/style.css" />
Run Code Online (Sandbox Code Playgroud)

并且加载失败

也导航到http://localhost:3000/_nuxt/assets/style.css 失败,但http://localhost:3000/_nuxt/assets/photo.jpg成功

笔记:

我不想将 style.css 放在 'static' 文件夹中,因为我想通过 webpack css-loader 加载它以添加缓存哈希

node.js vue.js vuejs2 nuxt.js

0
推荐指数
1
解决办法
2560
查看次数