Steps to use in Vue
Here are the steps to use in:
1. First, installation is required. You can install it via npm or yarn:
npm install immutable
or
yarn add immutable
2. Import Immutable in your Vue component:
import { Map, List } from 'immutable';
3. Use the data structure to create your state:
const state = Map({ todos: List([ Map({ id: 1, task: 'Learn Vue' }), Map({ id: 2, task: 'Learn Immutable' }) ]) });
4. Use this state in your Vue component:
export default { data() { return { state: state }; }, methods: { addTodo(task) { const newTodo = Map({ id: (), task }); = ('todos', list => (newTodo)); }, toggleDone(id) { = ('todos', list => { const todo = (todo => ('id') === id); return ((todo), ('done', done => !done)); }); } } };
5. Use this state in your Vue template:
<template> <div> <ul> <li v-for="todo in ('todos')" :key="('id')"> <input type="checkbox" v-model="('done')" @change="toggleDone(('id'))"> {{ ('task') }} </li> </ul> <input v-model="newTodo" type="text"> <button @click="addTodo(newTodo)">Add Todo</button> </div> </template>
In this example, we use ImmutableMap
andList
Data structures to manage our to-do list.
When adding a new to-do or changing the completion status of a to-do, we update our state, and since it is immutable, this will return a new state and the old state will remain the same.
In Vue templates, we can use this state as usual.
Used in Vue3
In Vue 3, you can still use it to manage the status of your usage program.
Here are the steps to use in Vue 3:
Install:
npm install immutable
2. Import Immutable in your Vue component:
import { Map, List } from 'immutable';
3. Use the data structure to create your status:
const state = Map({ todos: List([ Map({ id: 1, task: 'Learn Vue' }), Map({ id: 2, task: 'Learn Immutable' }) ]) });
4. Use this state in your Vue component:
import { reactive } from 'vue'; export default { setup() { const state = reactive(state); function addTodo(task) { const newTodo = Map({ id: (), task }); ('todos', list => (newTodo)); } function toggleDone(id) { ('todos', list => { const todo = (todo => ('id') === id); return ((todo), ('done', done => !done)); }); } return { state, addTodo, toggleDone }; } };
5. Use this state in your Vue template:
<template> <div> <ul> <li v-for="todo in " :key=""> <input type="checkbox" v-model="" @change="toggleDone()"> {{ }} </li> </ul> <input v-model="newTodo" type="text"> <button @click="addTodo(newTodo)">Add Todo</button> </div> </template>
In this example, we used Vue 3'sreactive
Function to create a responsive state object and use itMap
andList
Data structures to manage to-do lists.
When adding a new to-do or changing the completion status of a to-do, we update the state object, and since it is immutable, this returns a new state object, the old state object will remain unchanged.
In Vue templates, we can use this state object as usual.
This is all about this article about the use of Vue. For more related content on Vue, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!