Giter Site home page Giter Site logo

翻译表述有误 about docs-next-zh-cn HOT 5 CLOSED

xtyuns avatar xtyuns commented on August 15, 2024
翻译表述有误

from docs-next-zh-cn.

Comments (5)

Justineo avatar Justineo commented on August 15, 2024

原文所表达的意思应该是:当那些使用 key 的元素不再出现时将会被移除/销毁。

原翻译表达的确是有点歧义,但不是这样的。应该这样说:

那些使用了已经不存在的 key 的元素将会被移除/销毁。

from docs-next-zh-cn.

xtyuns avatar xtyuns commented on August 15, 2024

但是我通过以下案例测试了删除key对元素的影响,但是它并没有产生销毁元素的效果
(我不确定该案例是否能说明这个问题)

    <div id="app"></div>

    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.1.5/vue.global.min.js"></script>
    <script>
        Vue.createApp({
            setup: function() {
                const store =  Vue.reactive([
                    {id:0, v:0},
                    {id:1, v:1},
                    {id:2, v:2}
                ])

                Vue.onMounted(() => {
                    console.log('onMounted', Vue.toRaw(store))
                })
                
                const removeId = () => {
                    store.forEach(item => delete item.id)
                    console.log('delete id', Vue.toRaw(store))
                }

                return {store, removeId}
            },
            render() {
                return Vue.h('div', {}, [
                    Vue.h('ul', {}, this.store.map(item => Vue.h('li', { key: item.id }, item.v))),
                    Vue.h('button', { onClick: this.removeId }, 'remove Key')
                ])
            }
        }).mount('#app')
    </script>

from docs-next-zh-cn.

xtyuns avatar xtyuns commented on August 15, 2024

那些使用了已经不存在的 key 的元素将会被移除/销毁。

这个翻译确实是正确的,是我之前的理解有误

但是关于上面的那个案例,这的确使我不太能够理解,也有可能是我没有真正理解文档中所表述的概念。

from docs-next-zh-cn.

Yukon123 avatar Yukon123 commented on August 15, 2024

@xtyuns 因为 key的赋值过程是传递,而不是引用传递.所以删除 item.id 并不会影响到 key.
这也就是为什么vue推荐用 string | number类型来作为key的值

Don't use non-primitive values like objects and arrays as v-for keys. Use string or numeric values instead.
https://v3.vuejs.org/guide/list.html#maintaining-state

如果想做测试用例的话,应该是通过ref引用直接操作dom元素修改成新的不存在的key值
(这个列表的元素都有key值,我不确定创建一个没有key值的元素会不会被复用之前的元素,感觉最好是修改成新的key值)

from docs-next-zh-cn.

xtyuns avatar xtyuns commented on August 15, 2024
  1. 关于翻译的表述已经达成了一致。
  2. 关于 key 的使用问题我将私下再参考其他实例看一下。

稍后我将提交 PR 来关闭此 Issue。

from docs-next-zh-cn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.