最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to declare new map() by using ref in setup hook - vue 3 - Stack Overflow

programmeradmin0浏览0评论

I am migrating my vue 2 application into vue 3. While exploring position api, i came to know about ref function in setup hook which is replacing data function of option api.

In vue 2:

data() {
  return {
   cntmap: new Map()
  }
}

I am not sure how to initialize it in ref function of setup hook. I have started learning vue 3.

I am migrating my vue 2 application into vue 3. While exploring position api, i came to know about ref function in setup hook which is replacing data function of option api.

In vue 2:

data() {
  return {
   cntmap: new Map()
  }
}

I am not sure how to initialize it in ref function of setup hook. I have started learning vue 3.

Share Improve this question asked Feb 21, 2022 at 12:47 learningMonklearningMonk 1,4215 gold badges19 silver badges44 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Try like following snippet:

const { ref } = Vue
const app = Vue.createApp({
  setup() {
    const mapList = ref(new Map())
    
    mapList.value.set('a', 1);
    mapList.value.set('b', 2);
    mapList.value.set('c', 3);

    return { mapList }
  }
})
app.mount('#demo')
<script src="https://unpkg./[email protected]/dist/vue.global.prod.js"></script>
<div id="demo">
  <li v-for="item in mapList" :key="item">
    {{ item }}
  </li>
</div>

发布评论

评论列表(0)

  1. 暂无评论