1"use strict";(self.webpackChunk_inversifyjs_inversify_docs_site=self.webpackChunk_inversifyjs_inversify_docs_site||[]).push([["5649"],{9246(e,n,t){t.r(n),t.d(n,{metadata:()=>i,default:()=>j,frontMatter:()=>o,contentTitle:()=>d,toc:()=>l,assets:()=>c});var i=JSON.parse('{"id":"introduction/getting-started","title":"Getting started","description":"Start by installing inversify and reflect-metadata:","source":"@site/versioned_docs/version-6.x/introduction/getting-started.mdx","sourceDirName":"introduction","slug":"/introduction/getting-started","permalink":"/docs/6.x/introduction/getting-started","draft":false,"unlisted":false,"tags":[],"version":"6.x","sidebarPosition":1,"frontMatter":{"sidebar_position":1,"title":"Getting started"},"sidebar":"tutorialSidebar","next":{"title":"Dependency inversion","permalink":"/docs/6.x/introduction/dependency-inversion"}}'),a=t(89292),r=t(19736),s=t(48314);let o={sidebar_position:1,title:"Getting started"},d="Getting started",c={},l=[];function h(e){let n={a:"a",admonition:"admonition",code:"code",h1:"h1",header:"header",p:"p",pre:"pre",...(0,r.R)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(n.header,{children:(0,a.jsx)(n.h1,{id:"getting-started",children:"Getting started"})}),"\n",(0,a.jsxs)(n.p,{children:["Start by installing ",(0,a.jsx)(n.code,{children:"inversify"})," and ",(0,a.jsx)(n.code,{children:"reflect-metadata"}),":"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"npm install inversify reflect-metadata\n"})}),"\n",(0,a.jsx)(n.p,{children:"Next, initialize your first container and add some bindings:"}),"\n",(0,a.jsx)(n.admonition,{type:"warning",children:(0,a.jsxs)(n.p,{children:["Make sure to enable ",(0,a.jsx)(n.a,{href:"https://www.typescriptlang.org/tsconfig/#experimentalDecorators",children:"Experimental decorators"})," and ",(0,a.jsx)(n.a,{href:"https://www.typescriptlang.org/tsconfig/#emitDecoratorMetadata",children:"Emit Decorator Metadata"})," options in your ",(0,a.jsx)(n.code,{children:"tsconfig.json"})," to use this library."]})}),"\n",(0,a.jsx)(s.A,{language:"ts",children:"import { Container, inject, injectable } from 'inversify';\n\n@injectable()\nclass Katana {\n public readonly damage: number = 10;\n}\n\n@injectable()\nclass Ninja {\n constructor(\n @inject(Katana)\n public readonly katana: Katana,\n ) {}\n}\n\nconst container: Container = new Container();\n\ncontainer.bind(Ninja).toSelf();\ncontainer.bind(Katana).toSelf();\n\nconst ninja: Ninja = container.get(Ninja);\n\nconsole.log(ninja.katana.damage);"}),"\n",(0,a.jsxs)(n.p,{children:["The ",(0,a.jsx)(n.code,{children:"@injectable"})," decorator allows both ",(0,a.jsx)(n.code,{children:"Katana"})," and ",(0,a.jsx)(n.code,{children:"Ninja"})," classes to be used as container bindings. The ",(0,a.jsx)(n.code,{children:"@inject"})," decorator provides metadata about ",(0,a.jsx)(n.code,{children:"Ninja"})," dependencies, so the container knows that a ",(0,a.jsx)(n.code,{children:"Katana"})," should be provided as the first argument of ",(0,a.jsx)(n.code,{children:"Ninja"}),"'s constructor."]}),"\n",(0,a.jsxs)(n.p,{children:["Bindings are provided through the ",(0,a.jsx)(n.code,{children:"Container"})," API."]}),"\n",(0,a.jsx)(n.p,{children:"With these two steps, you are ready to initialize your very first ninja!"})]})}function j(e={}){let{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,a.jsx)(n,{...e,children:(0,a.jsx)(h,{...e})}):h(e)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.