小编Wes*_*ton的帖子

使用小胡子模板在Express中提供静态文件

我正在尝试从Express提供静态胡须文件的文件夹,但似乎无法弄清楚如何让它工作.假设我只有一个数据对象

{
  a: 'Hello :)'
  b: 'Goodbye :('
}
Run Code Online (Sandbox Code Playgroud)

还有两个文件,

public/a.html

<div>{{a}}</div>
Run Code Online (Sandbox Code Playgroud)

public/b.html

<div>{{b}}</div>
Run Code Online (Sandbox Code Playgroud)

我怎么能快速设置它为任意数量的静态html文件服务的位置,并用我的一个大对象替换模板化的部分?谢谢!

javascript middleware node.js mustache express

5
推荐指数
2
解决办法
1万
查看次数

将 YAML 解组为未知结构

对不起,标题令人困惑,我在措辞这个问题时遇到了麻烦。所以假设我有一个像这样的 YAML 配置文件

animals:
  -
    type: whale
    options:
      color: blue
      name: Mr. Whale
    features:
       -
         type: musician
         options:
           instruments:
             - Guitar
             - Violin
Run Code Online (Sandbox Code Playgroud)

非常人为的例子,但它直接类似于我真正使用的。

所以现在我有一些结构可以将这个配置编组到

type Config struct {
  AnimalConfigs []*AnimalConfig `yaml:"animals"`
}

type AnimalConfig struct{
  Type string
  Options map[string]string // ????
  Features []*FeatureConfig
}

type FeatureConfig struct{
  Type string
  Options ????
}
Run Code Online (Sandbox Code Playgroud)

所以这里的问题是动物类型(鲸鱼等)和特征(音乐家等)不是提前确定的,它们可以作为单独的模块添加,并且每个模块都可以有自己的配置。所以说有人正在使用这个库并想要添加他们自己的动物。我不知道这种动物是什么,它的选择是什么,它的特征是什么。我也不知道该功能的结构。我所知道的是它将有一个type财产,一个options财产。我希望开发人员能够添加自定义动物和功能,而我的库可以执行类似YourAnimal.Create(yourConfig).

我正在使用 go-yaml 库。正如您在 AnimalConfig 结构中看到的那样,我最初的想法是让optionsfeatures只是map[string]string,然后让自定义模块将该字符串解组到他们自己的结构中,但这不适用于该musician功能,因为它instruments是一个列表,不是字符串。谢谢!

yaml go

5
推荐指数
1
解决办法
2096
查看次数

在C++方法中返回对象的方法

假设我有一个看起来像这样的课程

class MyAnimals{
public:
    Animal getAnimal(int index){
        return animals.at(index);
    }
private:
    vector<Animal> animals;
}
Run Code Online (Sandbox Code Playgroud)

从我到目前为止学到的关于C++的知识,我认为getAnimal目前会返回动物的副本,而不是Java中的引用.我已经看到这被建议作为返回对象的正确方法,但是如果你想要在它返回后修改那个动物怎么办?我只是改变它的副本,MyAnimals.animals里面的实际动物将保持不变.我已经看到解决这个问题的一种方法是返回Animal&而不是Animal,这似乎在很大程度上起作用,但是如果我想重新分配给返回的Animal的变量怎么办?例如,

Animal& a = myanimals.getAnimal(1);
a = myanimals.getAnimal(2);
Run Code Online (Sandbox Code Playgroud)

从我得到的,这将改变动物myanimals.animals[1]是同一个确切的对象myanimals.animals[2],因为a是一个参考.返回物品有哪些不同的方法?

c++ reference class object

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

Creating a Rechart barchart with rounded edges

在此处输入图片说明

I'm trying to create a barchart like this, where the bar is X length, and each segment of the bar is proportional in length to the % of the data it contains. I've gotten pretty close by creating a vertical layout stacked barchart, but can't figure out how to get rounded edges. I've tried border={x} on the Bar components, but that rounds both sides of the bar, and I just want the left side of the first, and right side …

javascript reactjs recharts

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

管理对同一个 Apollo 突变的多次调用

所以看看文档中的 Apollo useMutation 示例https://www.apollographql.com/docs/react/data/mutations/#tracking-loading-and-error-states

function Todos() {
...
  const [
    updateTodo,
    { loading: mutationLoading, error: mutationError },
  ] = useMutation(UPDATE_TODO);
...

  return data.todos.map(({ id, type }) => {
    let input;

    return (
      <div key={id}>
        <p>{type}</p>
        <form
          onSubmit={e => {
            e.preventDefault();
            updateTodo({ variables: { id, type: input.value } });

            input.value = '';
          }}
        >
          <input
            ref={node => {
              input = node;
            }}
          />
          <button type="submit">Update Todo</button>
        </form>
        {mutationLoading && <p>Loading...</p>}
        {mutationError && <p>Error :( Please try again</p>}
      </div>
    );
  }); …
Run Code Online (Sandbox Code Playgroud)

javascript apollo reactjs graphql

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

如何禁用与自定义映射冲突的NERDTree映射?

我目前在.vimrc中具有此映射,以使在Windows之间导航更加容易。

nnoremap <c-j> <c-w>j

我注意到在安装NERDTree并使用将该文件在当前缓冲区中打开后e .,我再也无法使用它c-j向下移动到下面的窗口了。我看了一下NERDTree文档,发现了这个映射

<C-J>...Jump down to the next sibling of the current directory...|NERDTree-C-J|

我实在不明白自己使用这个映射,所以我怎么可能禁用它,回去<c-j>干什么<c-w>j

vim nerdtree

2
推荐指数
1
解决办法
406
查看次数

承诺拒绝在回调内部不起作用

我正在编写一个使用Google API的模块,但是在承诺中包装了所有基于回调的内容。这是问题区域的代码

file1.js

var File2 = require('file2')
var api = new File2()
api.auth().then(auth => {
  api.search('example').then(res => {
     ...do some stuff...
  })
}).catch(err => {
  console.log('1') //Not being run
  throw err
})
Run Code Online (Sandbox Code Playgroud)

file2.js

class File2(){
  auth() {
    ...works fine and resolves...
  }

  search() {
     return new Promise((resolve, reject) => {
       googleapi.somemethod(options, (err, res) => {
         if(err) { 
           console.log('2') // DOES run
           reject(new Error(err))
         }
         resolve(res.field) //Program crashes here because reject didn't actually reject
       })
     })

}
Run Code Online (Sandbox Code Playgroud)

对的调用auth工作正常,但是对search …

javascript error-handling callback node.js promise

1
推荐指数
2
解决办法
5003
查看次数