|
| 1 | +# Advanced Features |
| 2 | + |
| 3 | +This guide covers advanced LiveVue features and customization options. |
| 4 | + |
| 5 | +## Using ~V Sigil |
| 6 | + |
| 7 | +The `~V` sigil provides an alternative to the standard LiveView DSL, allowing you to write Vue components directly in your LiveView: |
| 8 | + |
| 9 | +```elixir |
| 10 | +defmodule MyAppWeb.CounterLive do |
| 11 | + use MyAppWeb, :live_view |
| 12 | + |
| 13 | + def render(assigns) do |
| 14 | + ~V""" |
| 15 | + <script setup lang="ts"> |
| 16 | + import {ref} from "vue" |
| 17 | + const props = defineProps<{count: number}>() |
| 18 | + const diff = ref<number>(1) |
| 19 | + </script> |
| 20 | +
|
| 21 | + <template> |
| 22 | + Current count: {{ props.count }} |
| 23 | + <input v-model="diff" type="range" min="1" max="10"> |
| 24 | + <button |
| 25 | + phx-click="inc" |
| 26 | + :phx-value-diff="diff" |
| 27 | + class="button"> |
| 28 | + Increment by {{ diff }} |
| 29 | + </button> |
| 30 | + </template> |
| 31 | + """ |
| 32 | + end |
| 33 | + |
| 34 | + def mount(_params, _session, socket) do |
| 35 | + {:ok, assign(socket, count: 0)} |
| 36 | + end |
| 37 | + |
| 38 | + def handle_event("inc", %{"diff" => diff}, socket) do |
| 39 | + {:noreply, update(socket, :count, &(&1 + String.to_integer(diff)))} |
| 40 | + end |
| 41 | +end |
| 42 | +``` |
| 43 | + |
| 44 | +## Lazy Loading Components |
| 45 | + |
| 46 | +Enable lazy loading by returning a function that returns a promise in your components configuration: |
| 47 | + |
| 48 | +```javascript |
| 49 | +// assets/vue/index.js |
| 50 | +const components = { |
| 51 | + Counter: () => import("./Counter.vue"), |
| 52 | + Modal: () => import("./Modal.vue") |
| 53 | +} |
| 54 | + |
| 55 | +// Using Vite's glob import |
| 56 | +const components = import.meta.glob( |
| 57 | + './components/*.vue', |
| 58 | + { eager: false, import: 'default' } |
| 59 | +) |
| 60 | +``` |
| 61 | + |
| 62 | +When SSR is enabled, related JS and CSS files will be automatically preloaded in HTML. |
| 63 | + |
| 64 | +## Customizing Vue App Instance |
| 65 | + |
| 66 | +You can customize the Vue app instance in `assets/vue/index.js`: |
| 67 | + |
| 68 | +```javascript |
| 69 | +import { createPinia } from "pinia" |
| 70 | +const pinia = createPinia() |
| 71 | + |
| 72 | +export default createLiveVue({ |
| 73 | + setup: ({ createApp, component, props, slots, plugin, el, ssr }) => { |
| 74 | + const app = createApp({ render: () => h(component, props, slots) }) |
| 75 | + app.use(plugin) |
| 76 | + app.use(pinia) // Add your plugins |
| 77 | + |
| 78 | + if (ssr) { |
| 79 | + // SSR-specific initialization |
| 80 | + } |
| 81 | + |
| 82 | + app.mount(el) |
| 83 | + return app |
| 84 | + } |
| 85 | +}) |
| 86 | +``` |
| 87 | + |
| 88 | +Available setup options: |
| 89 | + |
| 90 | +| Property | Description | |
| 91 | +|------------|------------------------------------------------| |
| 92 | +| createApp | Vue's createApp or createSSRApp function | |
| 93 | +| component | The Vue component to render | |
| 94 | +| props | Props passed to the component | |
| 95 | +| slots | Slots passed to the component | |
| 96 | +| plugin | LiveVue plugin for useLiveVue functionality | |
| 97 | +| el | Mount target element | |
| 98 | +| ssr | Boolean indicating SSR context | |
| 99 | + |
| 100 | +## Server-Side Rendering (SSR) |
| 101 | + |
| 102 | +LiveVue provides two SSR strategies: |
| 103 | + |
| 104 | +### Development (ViteJS) |
| 105 | +```elixir |
| 106 | +# config/dev.exs |
| 107 | +config :live_vue, |
| 108 | + ssr_module: LiveVue.SSR.ViteJS |
| 109 | +``` |
| 110 | +Uses Vite's ssrLoadModule for efficient development compilation. |
| 111 | + |
| 112 | +### Production (NodeJS) |
| 113 | +```elixir |
| 114 | +# config/prod.exs |
| 115 | +config :live_vue, |
| 116 | + ssr_module: LiveVue.SSR.NodeJS |
| 117 | +``` |
| 118 | +Uses elixir-nodejs for optimized production SSR with an in-memory server bundle. |
| 119 | + |
| 120 | +### SSR Performance |
| 121 | + |
| 122 | +Vue SSR is compiled into string concatenation for optimal performance. The SSR step: |
| 123 | +- Only runs during "dead" renders |
| 124 | +- Skips during live navigation |
| 125 | +- Can be disabled per-component with `v-ssr={false}` |
| 126 | + |
| 127 | +## Client-Side Hooks |
| 128 | + |
| 129 | +Access Phoenix hooks from Vue components using `useLiveVue`: |
| 130 | + |
| 131 | +```vue |
| 132 | +<script setup> |
| 133 | +import {useLiveVue} from "live_vue" |
| 134 | +
|
| 135 | +const hook = useLiveVue() |
| 136 | +
|
| 137 | +// Access all Phoenix hook methods |
| 138 | +hook.pushEvent("hello", {value: "world"}) |
| 139 | +hook.handleEvent("response", (payload) => { |
| 140 | + console.log(payload) |
| 141 | +}) |
| 142 | +</script> |
| 143 | +``` |
| 144 | + |
| 145 | +## TypeScript Support |
| 146 | + |
| 147 | +LiveVue provides full TypeScript support: |
| 148 | + |
| 149 | +1. Use the example tsconfig.json from the example project |
| 150 | +2. Check `example_project/assets/ts_config_example` for TypeScript versions of: |
| 151 | + - LiveVue entrypoint file |
| 152 | + - Tailwind configuration |
| 153 | + - Vite configuration |
| 154 | + |
| 155 | +For app.js TypeScript support: |
| 156 | +```javascript |
| 157 | +// app.js |
| 158 | +import {initApp} from './app.ts' |
| 159 | +initApp() |
| 160 | +``` |
| 161 | + |
| 162 | +## Next Steps |
| 163 | + |
| 164 | +- Check out the [FAQ](faq.html) for implementation details and optimization tips |
| 165 | +- Visit the [Deployment Guide](deployment.html) for production setup |
| 166 | +- Join our [GitHub Discussions](https://github.com/Valian/live_vue/discussions) for questions and ideas |
0 commit comments