小编Wen*_* Du的帖子

导出 * as bar VS 导出 { 默认为 bar }

两者有什么区别

export * as bar from 'foo'
Run Code Online (Sandbox Code Playgroud)

export { default as bar } from 'foo'
Run Code Online (Sandbox Code Playgroud)

在我的具体情况下,我尝试了以下两种方法,它们都有效,想知道它们之间的根本区别。

// echarts v5.0.0
export * as ECharts from 'echarts/lib/echarts.js'
export { default as ECharts } from 'echarts/lib/echarts.js'
Run Code Online (Sandbox Code Playgroud)

babel.config.js

module.exports = {
  // "@vue/cli-plugin-babel": "~4.5.0",
  presets: ['@vue/cli-plugin-babel/preset'],
}
Run Code Online (Sandbox Code Playgroud)

javascript

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

如何在 vue 3 脚本设置中使用 <component :is="">

我正在使用实验脚本设置来创建学习环境。我有一个自制的导航栏,打开一个组件。

我在使用该<component :is="" />方法时遇到问题。此方法在组件基础 -> 动态组件下的文档中进行了描述

在 Vue 3 Composition API 中,它按预期工作:

<template>
  <NavigationBar
    @switchTab="changeTab"
    :activeTab="tab"
  />
  <component :is="tab" />
</template>

<script>
import { ref } from 'vue'
import NavigationBar from './components/NavigationBar.vue'
import TemplateSyntax from './components/TemplateSyntax.vue'
import DataPropsAndMethods from './components/DataPropsAndMethods.vue'

export default {
  components: {
    NavigationBar,
    TemplateSyntax,
    DataPropsAndMethods
  },
  setup () {
    const tab = ref('DataPropsAndMethods')
    function changeTab (newTab) {
      tab.value = newTab
    }

    return {
      changeTab,
      tab
    }
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)

我的script …

javascript vue.js vuejs3 vue-composition-api vue-script-setup

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

Vue Router 推送错误:避免了到当前位置的冗余导航

有没有办法避免错误:避免冗余导航到当前位置。我需要进行分页,方法如下:

handlePageChange(page: number): void {
  const query = {
    ...this.$route.query,
    page: page.toString(),
  };
  this.$router.push({ name: 'PageName', query });
}
Run Code Online (Sandbox Code Playgroud)

我不断在控制台中收到错误:

Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: "/page-path?page=2".
Run Code Online (Sandbox Code Playgroud)

我尝试用路由器进行捕获,但这不起作用。有人可以帮助我阐明我在这里做错了什么吗?:/

javascript typescript vue.js vue-router

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

Vue 3模板引用动态名称

我注意到,要在 Vue 3 组合 api 中创建模板引用<script setup>,我应该创建一个与引用值完全相同的变量名称。

例如,在Vue 3 文档中:

<template>
  <div ref="root">This is a root element</div>   <--- notice the ref name "root"
</template>

<script>
  import { ref, onMounted } from 'vue'

  export default {
    setup() {
      const root = ref(null)   <---- and the variable name should be "root" too

      onMounted(() => {
        console.log(root.value) 
      })

      return {
        root
      }
    }
  }
</script>
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以像 Vue 2 那样使用 ref ,因为我有一些元素,其 ref 是随机字符串,并且它只需在$refs对象上传递名称即可在 Vue 2 上工作。

<div …
Run Code Online (Sandbox Code Playgroud)

vue.js vuejs3 vue-composition-api vue-script-setup

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

基于是否传递泛型类型的可选属性

如何创建一个接口,以便在T传递时a键入T,而在未传递时a可以是可选的(缺少属性),例如:

interface A<T = void> {
  a: T
}

const a1: A<string> = { a: 'a' } //  No error, good

const a2: A = {}

//  Should not throw
// "Property 'a' is missing in type '{}' but required in type 'A<void>'.ts(2741)"
Run Code Online (Sandbox Code Playgroud)

以下方法的唯一问题是a不能从对象中遗漏:

interface A<T = void> {
  a: T extends void ? undefined : T
}

const a3: A = {}

//  TS throws
// "Property 'a' is …
Run Code Online (Sandbox Code Playgroud)

typescript typescript-generics conditional-types

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

Prettier - 如何忽略 CLI 文件模式中的某些文件类型?

我只想排除jsjsxvue文件,我想像这样:

prettier --check --write --ignore-unknown "**/*.{!js,jsx,vue}"
Run Code Online (Sandbox Code Playgroud)

filepath filepattern prettier

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

`h` 和 `createVNode` 相同吗?

和 都h暴露createVNodevue

文档似乎表明它们是相同的:

h() 函数是创建 VNode 的实用程序。也许更准确地命名为createVNode()

但切换hcreateVNode会抛出:

<script lang="ts">
  import { createVNode, defineComponent, h } from 'vue'

  export default defineComponent({
    setup() {
      // works
      return () => h('strong', 'Foo')

      // throws
      return () => createVNode('strong', 'Foo')
    },
  })
</script>
Run Code Online (Sandbox Code Playgroud)

javascript vue.js virtual-dom vue-render-function vuejs3

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

如何使用`&lt;脚本设置&gt;`?

Vue版本:3.0.11

使用以下代码:

<template>
  <button @click="inc">{{ count }}</button>
</template>

<script setup>
  import { ref } from 'vue'

  export const count = ref(0)

  export const inc = () => count.value++
</script>
Run Code Online (Sandbox Code Playgroud)

我有:

ERROR  Failed to compile with 1 error
 error  in ./src/views/HyperScript.vue?vue&type=script&setup=true&lang=js

Syntax Error: TypeError: Cannot read property 'content' of null


 @ ./src/views/HyperScript.vue?vue&type=script&setup=true&lang=js 1:0-293 1:0-293 1:294-576 1:294-576
 @ ./src/views/HyperScript.vue
 @ ./src/router/index.ts
 @ ./src/main.ts
 @ multi (webpack)-dev-server/client?http://192.168.6.175:8080&sockPath=/sockjs-node (webpack)/hot/dev-server.js ./src/main.ts
Run Code Online (Sandbox Code Playgroud)

javascript vue.js vuejs3 vue-composition-api vue-script-setup

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

Vector3 转纬度经度

我正在尝试对球体上的一个点进行光线投射,然后将该点转换为其适当的纬度和经度。我遇到了这个问题,它与我在堆栈交换上所做的事情几乎完全相关

球体上的 3D 坐标为纬度和经度

使用这个函数

public static LatLon FromVector3(Vector3 position, float sphereRadius)
    {
        float lat = (float)Math.Acos(position.Y / sphereRadius); //theta
        float lon = (float)Math.Atan(position.X / position.Z); //phi
        return new LatLon(lat, lon);
    }
Run Code Online (Sandbox Code Playgroud)

但是,该函数始终返回 0 到 3(纬度)和 -1 到 1(经度)范围内的值。

我使用的程序是 Unity,有谁知道为什么我可能会得到这些不正确的值?我将光线投射命中世界矢量和球体半径作为参数传递,并且球体以世界 0,0,0 为中心。

更新代码

public static Vector2 GetLatLonFromVector3 (Vector3 position, float sphereRadius){
        float lat = (float)Mathf.Acos(position.y / sphereRadius); //theta
        float lon = (float)Mathf.Atan2(position.x, position.z); //phi
        lat *= Mathf.Rad2Deg;
        lon *= Mathf.Rad2Deg;
        return new Vector2(lat, lon);
    }
Run Code Online (Sandbox Code Playgroud)

我现在遇到的问题是它们所在的象限没有正确反映这些值。

geometry latitude-longitude unity-game-engine

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

如何在 C++ 中反转动态数组?

这是我第一次尝试反转动态数组:

bool reverse()
{
    T *newArray = NULL;
            
    // Validate operation.
    if (!isValid() || isReadOnly())
        return false;

    // Allocate new array
    newArray = new (std::nothrow)T[m_size];
    if (newArray == NULL)
        return false;

    // Reverse the array's contents.
    for (int i = m_size - 1; i >= 0; i--)
        newArray[i] = m_array[i];

    // Delete old array.
    delete[] m_array; 
    m_array = NULL;

    // Assign new array 
    m_array = newArray; 
        
    return true;
}
Run Code Online (Sandbox Code Playgroud)

可以想象,这对于大型数组来说是非常昂贵的:

  • 分配和解除分配需要时间。
  • 带有“for”的线性算法也需要时间。

我知道 std::reverse,但不幸的是它不适用于动态数组。

我应该使用 std::vector 吗?是的。但这是为了学习。我正在阅读一本数据结构游戏编程书籍并扩展我的学习。

所以我有兴趣将 Array 的这个成员函数简化为算法本身: …

c++ arrays

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