|
| 1 | +# Adapt |
| 2 | + |
| 3 | +If you want to use Mettle in an application project developed with another front-end framework, that's fine too. Mettle is very flexible and can be easily adapted. |
| 4 | + |
| 5 | +## Vue |
| 6 | + |
| 7 | +**main.js** |
| 8 | + |
| 9 | +```js |
| 10 | +import { createApp } from 'vue'; |
| 11 | +import { createPinia } from 'pinia'; |
| 12 | +import App from './App.vue'; |
| 13 | +import mettle from './mettle-plugin'; |
| 14 | + |
| 15 | +const pinia = createPinia(); |
| 16 | +const app = createApp(App); |
| 17 | + |
| 18 | +app.use(pinia); |
| 19 | +app.mount('#app'); |
| 20 | + |
| 21 | +app.use(mettle); |
| 22 | +``` |
| 23 | + |
| 24 | +**App.vue** |
| 25 | + |
| 26 | +```vue |
| 27 | +<script setup> |
| 28 | +import { ref,h } from 'vue' |
| 29 | +import { useCounterStore } from './store'; |
| 30 | +
|
| 31 | +const counterStore = useCounterStore() |
| 32 | +const add = ()=>{ |
| 33 | + counterStore.increment() |
| 34 | +} |
| 35 | +</script> |
| 36 | +
|
| 37 | +<template> |
| 38 | + <div> |
| 39 | + <button @click="add">add</button> |
| 40 | + <div id="mettle-inner"></div> |
| 41 | + </div> |
| 42 | +</template> |
| 43 | +
|
| 44 | +``` |
| 45 | + |
| 46 | +**mettle-plugin.jsx** |
| 47 | + |
| 48 | +```jsx |
| 49 | +import { createApp } from 'mettle'; |
| 50 | +import App from '@/mettle/App.jsx'; |
| 51 | + |
| 52 | +export default { |
| 53 | + install: () => { |
| 54 | + createApp(<App />, '#mettle-inner'); |
| 55 | + }, |
| 56 | +}; |
| 57 | +``` |
| 58 | + |
| 59 | +**mettle/App.jsx** |
| 60 | + |
| 61 | +```jsx |
| 62 | +import { watch } from 'vue'; |
| 63 | +import { useCounterStore } from '@/store'; |
| 64 | + |
| 65 | +function App({ setData }) { |
| 66 | + const counterStore = useCounterStore(); |
| 67 | + |
| 68 | + watch( |
| 69 | + () => counterStore.count, |
| 70 | + (newVal) => { |
| 71 | + console.log(newVal); |
| 72 | + setData(); |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + return () => ( |
| 77 | + <fragment> |
| 78 | + <h1>{counterStore.count}</h1> |
| 79 | + <h2>{counterStore.doubleCount}</h2> |
| 80 | + </fragment> |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +export default App; |
| 85 | +``` |
| 86 | + |
| 87 | +**store/index.js** |
| 88 | + |
| 89 | +```js |
| 90 | +import { defineStore } from 'pinia'; |
| 91 | +import { ref, computed } from 'vue'; |
| 92 | + |
| 93 | +export const useCounterStore = defineStore('counter', () => { |
| 94 | + const count = ref(0); |
| 95 | + const name = ref('Eduardo'); |
| 96 | + const doubleCount = computed(() => count.value * 2); |
| 97 | + function increment() { |
| 98 | + count.value++; |
| 99 | + } |
| 100 | + |
| 101 | + return { count, name, doubleCount, increment }; |
| 102 | +}); |
| 103 | +``` |
0 commit comments