vuex-asr
Version:
* Version: 0.7.52 * License M.I.T. * [Online Documentation](https://vuex-asr.github.io/vuex-asr/) * author: Joris Wenting * email: vuex.asr@gmail.com * [linkedIn](https://www.linkedin.com/in/joriswenting/). * [contribute](https://vuex-asr.github.io/vuex-a
55 lines (48 loc) • 17.4 kB
HTML
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>passing a binding to a child component | Vuex ASR</title>
<meta name="description" content="Automated store resolution for Vue.js">
<link rel="icon" href="../vue-logo.png">
<link rel="preload" href="/vuex-asr/assets/css/0.styles.ba1a25a1.css" as="style"><link rel="preload" href="/vuex-asr/assets/js/app.51a1fadf.js" as="script"><link rel="preload" href="/vuex-asr/assets/js/2.b4a928f2.js" as="script"><link rel="preload" href="/vuex-asr/assets/js/36.b062e03a.js" as="script"><link rel="prefetch" href="/vuex-asr/assets/js/10.d050881c.js"><link rel="prefetch" href="/vuex-asr/assets/js/11.4a0808ab.js"><link rel="prefetch" href="/vuex-asr/assets/js/12.3c479c42.js"><link rel="prefetch" href="/vuex-asr/assets/js/13.3250c310.js"><link rel="prefetch" href="/vuex-asr/assets/js/14.62c5e3bf.js"><link rel="prefetch" href="/vuex-asr/assets/js/15.1246035b.js"><link rel="prefetch" href="/vuex-asr/assets/js/16.f2a6d574.js"><link rel="prefetch" href="/vuex-asr/assets/js/17.c0c4ba46.js"><link rel="prefetch" href="/vuex-asr/assets/js/18.7f284ab7.js"><link rel="prefetch" href="/vuex-asr/assets/js/19.af7a8adf.js"><link rel="prefetch" href="/vuex-asr/assets/js/20.d5e4f501.js"><link rel="prefetch" href="/vuex-asr/assets/js/21.4073ea7c.js"><link rel="prefetch" href="/vuex-asr/assets/js/22.46346157.js"><link rel="prefetch" href="/vuex-asr/assets/js/23.4ac41cbb.js"><link rel="prefetch" href="/vuex-asr/assets/js/24.72b4b854.js"><link rel="prefetch" href="/vuex-asr/assets/js/25.3f626a44.js"><link rel="prefetch" href="/vuex-asr/assets/js/26.ed8ed3d3.js"><link rel="prefetch" href="/vuex-asr/assets/js/27.8c51c981.js"><link rel="prefetch" href="/vuex-asr/assets/js/28.86d49fe1.js"><link rel="prefetch" href="/vuex-asr/assets/js/29.4ef76ef5.js"><link rel="prefetch" href="/vuex-asr/assets/js/3.2b8ad3c3.js"><link rel="prefetch" href="/vuex-asr/assets/js/30.7ad3ecff.js"><link rel="prefetch" href="/vuex-asr/assets/js/31.a8eb0654.js"><link rel="prefetch" href="/vuex-asr/assets/js/32.eb4c2bac.js"><link rel="prefetch" href="/vuex-asr/assets/js/33.00186259.js"><link rel="prefetch" href="/vuex-asr/assets/js/34.7b77e1ee.js"><link rel="prefetch" href="/vuex-asr/assets/js/35.26de21a8.js"><link rel="prefetch" href="/vuex-asr/assets/js/37.bdead487.js"><link rel="prefetch" href="/vuex-asr/assets/js/38.2e2b47dd.js"><link rel="prefetch" href="/vuex-asr/assets/js/4.6cc4b15c.js"><link rel="prefetch" href="/vuex-asr/assets/js/5.51ffd98f.js"><link rel="prefetch" href="/vuex-asr/assets/js/6.f1224752.js"><link rel="prefetch" href="/vuex-asr/assets/js/7.3659af3a.js"><link rel="prefetch" href="/vuex-asr/assets/js/8.366b1b49.js"><link rel="prefetch" href="/vuex-asr/assets/js/9.af028c38.js">
<link rel="stylesheet" href="/vuex-asr/assets/css/0.styles.ba1a25a1.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/vuex-asr/" class="home-link router-link-active"><!----> <span class="site-name">Vuex ASR</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/vuex-asr/" class="nav-link">Home</a></div><div class="nav-item"><a href="/vuex-asr/step-by-step-guide/hello-world-example.html" class="nav-link">Guide</a></div><div class="nav-item"><a href="/vuex-asr/helpers/cheat-sheet.html" class="nav-link">Cheatsheet</a></div><div class="nav-item"><a href="/vuex-asr/helpers/contribute.html" class="nav-link">Contribute</a></div><div class="nav-item"><a href="https://github.com/vuex-asr/vuex-asr" target="_blank" rel="noopener noreferrer" class="nav-link external">
Github
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/vuex-asr/" class="nav-link">Home</a></div><div class="nav-item"><a href="/vuex-asr/step-by-step-guide/hello-world-example.html" class="nav-link">Guide</a></div><div class="nav-item"><a href="/vuex-asr/helpers/cheat-sheet.html" class="nav-link">Cheatsheet</a></div><div class="nav-item"><a href="/vuex-asr/helpers/contribute.html" class="nav-link">Contribute</a></div><div class="nav-item"><a href="https://github.com/vuex-asr/vuex-asr" target="_blank" rel="noopener noreferrer" class="nav-link external">
Github
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Introduction</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vuex-asr/" class="sidebar-link">vuex-asr</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Step by step guide</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vuex-asr/step-by-step-guide/prerequisite.html" class="sidebar-link">prerequisite</a></li><li><a href="/vuex-asr/step-by-step-guide/installation.html" class="sidebar-link">installation</a></li><li><a href="/vuex-asr/step-by-step-guide/hello-world-example.html" class="sidebar-link">hello world example</a></li><li><a href="/vuex-asr/step-by-step-guide/getters-example.html" class="sidebar-link">hello world example with a getter</a></li><li><a href="/vuex-asr/step-by-step-guide/hello-world-of-2-way-binding.html" class="sidebar-link">hello world of 2 way binding</a></li><li><a href="/vuex-asr/step-by-step-guide/mutations.html" class="sidebar-link">mutations</a></li><li><a href="/vuex-asr/step-by-step-guide/actions.html" class="sidebar-link">actions</a></li><li><a href="/vuex-asr/step-by-step-guide/aliasing.html" class="sidebar-link">aliasing</a></li><li><a href="/vuex-asr/step-by-step-guide/namespacing.html" class="sidebar-link">namespacing</a></li><li><a href="/vuex-asr/step-by-step-guide/passing-bindings.html" class="active sidebar-link">passing a binding to a child component</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/vuex-asr/step-by-step-guide/passing-bindings.html#example" class="sidebar-link">example</a></li><li class="sidebar-sub-header"><a href="/vuex-asr/step-by-step-guide/passing-bindings.html#next-steps" class="sidebar-link">next steps</a></li></ul></li><li><a href="/vuex-asr/step-by-step-guide/binding-multiple-objects-to-a-component.html" class="sidebar-link">binding multiple objects to a component</a></li><li><a href="/vuex-asr/step-by-step-guide/binding-a-configuration.html" class="sidebar-link">binding a configuration</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Helpers</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/vuex-asr/helpers/asr-debug.html" class="sidebar-link">asr-debug</a></li><li><a href="/vuex-asr/helpers/working-with-the-test-suite.html" class="sidebar-link">working with the test-suite</a></li><li><a href="/vuex-asr/helpers/cheat-sheet.html" class="sidebar-link">cheatsheet</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="passing-a-binding-to-a-child-component"><a href="#passing-a-binding-to-a-child-component" aria-hidden="true" class="header-anchor">#</a> passing a binding to a child component</h1> <p>An important feature of the vuex-asr plugin is the ability to pass model-bindings to a child component.</p> <p>This means that a model that has already been bound to the component, either via vuex-asr or conventionally in the component itself, can also be passed to a child component. Giving you ultimate flexibility in organising and generalising your codebase.</p> <div class="tip custom-block"><p class="custom-block-title">TIP</p> <p>You could use <code>asr-pass</code> in existing vue projects without using other vuex-asr functionality, to pass a binding from a parent- to a child-component.</p></div> <div class="tip custom-block"><p class="custom-block-title">codesandbox</p> <p><a href="https://codesandbox.io/s/manual-asr-pass-nvcd5" target="_blank" rel="noopener noreferrer">asr-pass<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></div> <h2 id="example"><a href="#example" aria-hidden="true" class="header-anchor">#</a> example</h2> <p>So let's say we bind a model in our App file to a component <code><pass-example-component></code>:</p> <div class="language-vue extra-class"><div class="highlight-lines"><br><br><br><br><div class="highlighted"> </div><br><br><br><br><br></div><pre class="language-vue"><code> // src/App.vue
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>app<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>pass-example-component</span> <span class="token attr-name">asr-bind-state</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>message IS v-model<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span>
...
</code></pre></div><p>And pass it inside <code><pass-example-component></code> to <code><text-input></code>:</p> <div class="language-vue extra-class"><div class="highlight-lines"><br><br><br><br><br><br><br><div class="highlighted"> </div><br><br><br><br><br></div><pre class="language-vue"><code> // src/components/ExampleMessage.vue
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>pass-example-component<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>Print it in this component<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
{{ message }}
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>And pass it from here to a child component<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>text-input</span> <span class="token attr-name">asr-pass</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>message<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span>
...
</code></pre></div><p>Notice the <code>asr-pass</code> attribute, since message was bound to this component it is available to be passed to a child component.</p> <div class="language-vue extra-class"><div class="highlight-lines"><br><br><br><br><br><div class="highlighted"> </div><div class="highlighted"> </div><br><br><br></div><pre class="language-vue"><code> // src/components/TextInput.vue
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>text-input<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h1</span><span class="token punctuation">></span></span>This is the child with the variable "message" passed:<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h1</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>{{ message }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>input</span> <span class="token attr-name">v-model</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>message<span class="token punctuation">"</span></span><span class="token punctuation">/></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span>
</code></pre></div><p>There's nothing more to it.</p> <h2 id="next-steps"><a href="#next-steps" aria-hidden="true" class="header-anchor">#</a> next steps</h2> <p>In the next chapter we take a look at how to bind multiple state items.</p></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/vuex-asr/step-by-step-guide/namespacing.html" class="prev">namespacing</a></span> <span class="next"><a href="/vuex-asr/step-by-step-guide/binding-multiple-objects-to-a-component.html">binding multiple objects to a component</a>→
</span></p></div> </main></div><div class="global-ui"></div></div>
<script src="/vuex-asr/assets/js/app.51a1fadf.js" defer></script><script src="/vuex-asr/assets/js/2.b4a928f2.js" defer></script><script src="/vuex-asr/assets/js/36.b062e03a.js" defer></script>
</body>
</html>