小编Pet*_* Zo的帖子

Vue3、Typescript 和 Eslint 引发:“解析错误:预期为 '}'”

我正在使用 Vue3 和 Typescript 编写代码,这是 的代码App.vue,它是根组件:

<template>
  <router-view v-if="inited" />
  <div v-else>
    Initing...
  </div>
</template>

<script lang="ts">
import router from './router';
import { defineComponent } from 'vue';
import { useStore } from 'vuex';
import { key } from './store';

const store = useStore(key);

export default defineComponent({
  data() {
    return { inited: store.state.inited };
  },
});
</script>
Run Code Online (Sandbox Code Playgroud)

但他们eslint告诉我:

/home/peter/proj/skogkatt-next/src/App.vue
  17:9  error  Parsing error: '}' expected
Run Code Online (Sandbox Code Playgroud)

我在Google等上使用了很多时间,但仍然找不到有用的解决方案。这是 eslint 的配置package.json

/home/peter/proj/skogkatt-next/src/App.vue
  17:9  error  Parsing error: '}' expected …
Run Code Online (Sandbox Code Playgroud)

eslint vue.js

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

没有语法糖的带参数的装饰器相当于什么?

我正在学习装饰器,并遇到了一个装饰器争论的例子。不过,这对我来说有点困惑,因为我了解到(注意:这个问题的例子 主要来自这篇文章):

def my_decorator(func):
  def inner(*args, **kwargs):
    print('Before function runs')
    func(*args, **kwargs)
    print('After function ran')
  return inner

@my_decorator
def foo(thing_to_print):
  print(thing_to_print)

foo('Hello')
# Returns:
# Before function runs
# Hello
# After function ran
Run Code Online (Sandbox Code Playgroud)

相当于

foo = my_wrapper(foo)
Run Code Online (Sandbox Code Playgroud)

所以,对我来说,有些东西如何接受参数是没有意义的,为了更好地解释,这是一个接受参数的装饰器示例:

def repeat(num_times):
    def decorator_repeat(func):
        @functools.wraps(func)
        def wrapper_repeat(*args, **kwargs):
            for _ in range(num_times):
                value = func(*args, **kwargs)
            return value
        return wrapper_repeat
    return decorator_repeat

@repeat(num_times=4)
def greet(name):
    print(f"Hello {name}")

greet('Bob')
# Returns:
# Hello Bob
# Hello Bob
# …
Run Code Online (Sandbox Code Playgroud)

python python-decorators

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

如何通过 Rust 的约简得到向量的乘积?

我知道如何使用fold来获取产品:

let product = V.iter().fold(1, |res, a| res * a);
Run Code Online (Sandbox Code Playgroud)

但我不知道如何获得该产品reduce。我知道我可以使用以下方法into_iter

let produce = V.into_iter().reduce(|a, b| a * b).unwrap();
Run Code Online (Sandbox Code Playgroud)

into_iter会感动主人。如果我只使用iter方法,它会引发错误......

 $ rustc main.rs
error[E0308]: mismatched types
 --> main.rs:3:42
  |
3 |     let produce = V.iter().reduce(|a, b| a * b).unwrap();
  |                                          ^^^^^
  |                                          |
  |                                          expected `&{integer}`, found integer
  |                                          help: consider borrowing here: `&(a * b)`

error: aborting due to previous error

For more information about this error, try `rustc …
Run Code Online (Sandbox Code Playgroud)

rust

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

标签 统计

eslint ×1

python ×1

python-decorators ×1

rust ×1

vue.js ×1